Why is this String suddenly null in my chat_gpt_sdk run?

41 Views Asked by At

I am trying to get messages from a thread using the chat_gpt_sdk flutter package. I am failing at a seemingly very simple step. I must be missing something fundamental and it's driving me nuts.

Here is a piece of my code:

final runRequest = CreateRun(assistantId: assistantID);
    runResult = await globals.chatAI.threads.runs
        .createRun(threadId: threadID, request: runRequest);
    
    var runID = runResult.id.toString();
    print('runID: $runID');
    // this is giving me the output 'runID: run_abc123'.

    final mRunSteps = await globals.chatAI.threads.runs.listRunSteps(
      threadId: threadID,
      runId: runID, //this is where I get an error!
    );

    print('runID after: ${runResult.id}');

If I run the code above, I get a runtime error saying

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
#0      new ListRun.fromJson (package:chat_gpt_sdk/src/model/run/response/list_run.dart:19:22)
#1      OpenAIClient.get (package:chat_gpt_sdk/src/client/openai_client.dart:59:25)
<asynchronous suspension>
#2      ChatBotBrain.getChatAnswer (package:germaniac01/controlers/chatbot_brain.dart:84:23)
<asynchronous suspension>
#3      _ChatBotScreenState.build.<anonymous closure>.<anonymous closure> (package:germaniac01/screens/chatbot_screen.dart:52:35)
<asynchronous suspension>

The error is pointing to the position before the await globals.chatAI.threads..... I assume it's related to the runID, because if I exchange the definition of runID with runID = 'run_abc123' instead, it works just fine. In that case the print below gives me the same output as the one above, so it seems that runID is a String all the time. If I wrap it with if(runID is String) it says that's unnecessary bc always true.

Why does it say it is null then? What am I missing?

1

There are 1 best solutions below

6
EdwynZN On

I think the error is not on your side. maybe we can debug it together. OpenAI api says the response of that endpoint looks like this:

{
  "object": "list",
  "data": [
    {
      "id": "step_abc123",
      "object": "thread.run.step",
      "created_at": 1699063291,
      "run_id": "run_abc123",
      "assistant_id": "asst_abc123",
      "thread_id": "thread_abc123",
      "type": "message_creation",
      "status": "completed",
      "cancelled_at": null,
      "completed_at": 1699063291,
      "expired_at": null,
      "failed_at": null,
      "last_error": null,
      "step_details": {
        "type": "message_creation",
        "message_creation": {
          "message_id": "msg_abc123"
        }
      },
      "usage": {
        "prompt_tokens": 123,
        "completion_tokens": 456,
        "total_tokens": 579
      }
    }
  ],
  "first_id": "step_abc123", ///this is the problem apparently
  "last_id": "step_abc456",
  "has_more": false
}

The error in your app says its in this files:

#0      new ListRun.fromJson (package:chat_gpt_sdk/src/model/run/response/list_run.dart:19:22)
#1      OpenAIClient.get (package:chat_gpt_sdk/src/client/openai_client.dart:59:25)

The #1 is of the client trying to make the request, and #0 is the model trying to convert the json to the object:

factory ListRun.fromJson(Map<String, dynamic> json) => ListRun(
        firstId: json["first_id"], /// this is the line 19 of that file

can you check/debug with the flutter debug tool in the network view what is the response (it should be 200 with a json and then compare it with the one here, the endpoint has to be something like /$threadId/$kRuns/run_abc123/steps)

enter image description here

This is an example of the network view. It says first_id shoudn't be null but it looks like openAI is responding with null.

Is this your fist question in the openAI chat? do you actually have a run instance at that moment?