Hello Guys, I am using structured output of OpenAI to get response instructured way, and I am further I am using streaming to improve the response and send faster chunks response to client.
with client.beta.chat.completions.stream(
model=“gpt-4o-mini-2024-07-18”,
messages=user_messages,
response_format=ViniReply, stream_options={“include_usage”: True}
) as stream:
this is my client initialization part, ViniReply is mt structured response class.
and I am processing chunks as
for chunk in stream:
rest logic - mainly to parse json I’m doing like
if chunk.type == 'chunk':
# chunk_dict = chunk.to_dict()
# latest_snapshot = chunk_dict['snapshot']
# The first chunk doesn't have the 'parsed' key, so using .get to prevent raising an exception
# latest_parsed = latest_snapshot['choices'][0]['message'].get('parsed', {})
# # Note that usage is not available until the final chunk
# latest_usage = latest_snapshot.get('usage', {})
# latest_json = latest_snapshot['choices'][0]['message']['content']
But sometimes I am getting EOF exception while parsing the chunk in stream.
I recieve two chunks -
ChunkEvent(type=‘chunk’, chunk=ChatCompletionChunk(id=‘chatcmpl-AG4GJodbMopAZklL13lYvsDZn7lfu’, choices=[Choice(delta=ChoiceDelta(content=‘’, function_call=None, refusal=None, role=‘assistant’, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1728392995, model=‘gpt-4o-mini-2024-07-18’, object=‘chat.completion.chunk’, service_tier=None, system_fingerprint=‘fp_f85bea6784’, usage=None), snapshot=ParsedChatCompletion[object](id=‘chatcmpl-AG4GJodbMopAZklL13lYvsDZn7lfu’, choices=[ParsedChoice[object](finish_reason=None, index=0, logprobs=None, message=ParsedChatCompletionMessage[object](content=‘’, refusal=None, role=‘assistant’, function_call=None, tool_calls=None, parsed=None))], created=1728392995, model=‘gpt-4o-mini-2024-07-18’, object=‘chat.completion’, service_tier=None, system_fingerprint=‘fp_f85bea6784’, usage=None))
and then,
ChunkEvent(type=‘chunk’, chunk=ChatCompletionChunk(id=‘chatcmpl-AG4OfJriuKOS9AA8rCGRfkL1Hvcb4’, choices=[Choice(delta=ChoiceDelta(content=‘’, function_call=None, refusal=None, role=‘assistant’, tool_calls=None), finish_reason=None, index=0, logprobs=None)], created=1728393513, model=‘gpt-4o-mini-2024-07-18’, object=‘chat.completion.chunk’, service_tier=None, system_fingerprint=‘fp_f85bea6784’, usage=None), snapshot=ParsedChatCompletion[object](id=‘chatcmpl-AG4OfJriuKOS9AA8rCGRfkL1Hvcb4’, choices=[ParsedChoice[object](finish_reason=None, index=0, logprobs=None, message=ParsedChatCompletionMessage[object](content=‘’, refusal=None, role=‘assistant’, function_call=None, tool_calls=None, parsed=None))], created=1728393513, model=‘gpt-4o-mini-2024-07-18’, object=‘chat.completion’, service_tier=None, system_fingerprint=‘fp_f85bea6784’, usage=None))
after this I am getting this exception -
Exception in call_vini - Traceback (most recent call last):
File “/Users/arpit/Documents/VIRTUAL-FRIEND/sakura_va/service/structured_output_openai_service.py”, line 53, in call_vini
for chunk in stream:
File “/Users/arpit/miniconda3/envs/myenv/lib/python3.10/site-packages/openai/lib/streaming/chat/_completions.py”, line 72, in iter
for item in self._iterator:
File “/Users/arpit/miniconda3/envs/myenv/lib/python3.10/site-packages/openai/lib/streaming/chat/_completions.py”, line 116, in stream
events_to_fire = self._state.handle_chunk(sse_event)
File “/Users/arpit/miniconda3/envs/myenv/lib/python3.10/site-packages/openai/lib/streaming/chat/_completions.py”, line 317, in handle_chunk
self.__current_completion_snapshot = self._accumulate_chunk(chunk)
File “/Users/arpit/miniconda3/envs/myenv/lib/python3.10/site-packages/openai/lib/streaming/chat/_completions.py”, line 407, in _accumulate_chunk
choice_snapshot.message.parsed = from_json(
ValueError: EOF while parsing a value at line 2 column 0