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?
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
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)
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.