How to get API response in JSON format

I am calling the Assistant API using Python SDK and all response are in some custom object serialization. So far, I have been extracting the values using regex but I realized this might cause issue in the long run.

Example of the response:

SyncCursorPage[ThreadMessage](data=[ThreadMessage(id='msg_n5NayPsadasdsadfvN2Uk8fdHhoG6v9wDJ3p2', assistant_id='asst_L3MnMdkqraprGqddfasdasdZA4uWIZeF', content=[MessageContentText(text=Text(annotations=[], value="Hey there! If you're looking for help or have a question, just type it out, and I'll do my best to assist you. Whether it's about the files you've uploaded or something else entirely, I'm here to provide support and information. Let's get started on what you need—just share some details with me."), type='text')], created_at=1706218934, file_ids=[], metadata={}, object='thread.message', role='assistant', run_id='run_yi2mr4QxNgSqJbsa6Sb1sfaCdCbm', thread_id='thread_jtm1axlrh2DhKVn8o6bsafsdfrjn9M'),

I haven’t been able to find a good way to parse this format into json format.

Is there a way to get response in JSON or convert this format into Object that has keys and values?

1 Like

I am also up against the same issue. Assistant in the playground will return a very well-structured JSON when I include only provide an RFC8259 compliant JSON response in the prompt, but the exact same prompts via the API from Python code will fetch me results similar to what you pasted above.

I am almost certain that there is a way to do this, but I have not found it yet. Also, writing parsing code is impossible as the response changes quite a bit in terms of the keys it contains.

I will post back if I find something but hopefully, someone has already got this sorted and let us know :slight_smile:

Not super familiar with the assistants API but it seems like they provide and example on how to parse it in this tutorial here.

(sorry in advance if I’ve misunderstood your question and that’s not what you’re asking about)

1 Like

You got the ask right, but as I mentioned in Playground, everything works fine, but when the same prompts are submitted to the API directly, the results are different. There may be some formatting that Playground does (/though looking at their logs, I did not find anything clear)

The tutorial there is also about the api.

I actually think your question might be different than the OPs (which is the one I was answering) :sweat_smile:

1 Like

For anyone who doesn’t want to read the entire tutorial mentioned by @ramn7 :

message_list = client.beta.threads.messages.list(thread_id=openai_thread_id).model_dump_json()

Pretty printing helper

def pretty_print(messages):
print(“# Messages”)
for m in messages:
print(f"{m.role}: {m.content[0].text.value}")
print()
This helper will grab everything in human readable format.

A slow solution but 100% reliable that you might consider is to enable “Code interpreter” in your assistant and instruct the assistant to write the result in a JSON file. Then you can get the content of this JSON file created by the assistant.

1 Like