Parse Structured Streaming Responses with Metadata in useChat #4466
-
Hello, I am using useChat in my React app to create a conversational UI for a chatbot application. The backend is built with Python Django REST API and does not use However, we are updating the response to include additional metadata. The new streaming response now looks like this:
This response can be accessed via message.content. However, I can't directly access properties like text or messageId from {message.content} because useChat returns a structured response with role and content. Here is an example of the messages object structure:
Could someone guide me on where I should parse the response? Should I handle this parsing inside the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Will the python server be streaming invalid json until complete? If so, you'll have a hard time displaying a real-time ui streaming the content to the user as parsing the json will fail until the end the json is valid. In that scenario onFinish would be the right method to hook into (onResponse triggers on the first response from the server not when the stream ends) You could also memo-ize and map the messages array and call JSON.parse on prior to rendering the message list. Assuming the content streams in, the JSON.parse will fail until it's valid json, so you'll want to try/catch and display a loading |
Beta Was this translation helpful? Give feedback.
Will the python server be streaming invalid json until complete? If so, you'll have a hard time displaying a real-time ui streaming the content to the user as parsing the json will fail until the end the json is valid. In that scenario onFinish would be the right method to hook into (onResponse triggers on the first response from the server not when the stream ends)
You could also memo-ize and map the messages array and call JSON.parse on prior to rendering the message list. Assuming the content streams in, the JSON.parse will fail until it's valid json, so you'll want to try/catch and display a loading