fix(internal): handle leading whitespace in message content #2083
+1
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes being requested
This PR fixes an issue in the GPT structured output streaming functionality where unexpected leading whitespace (e.g., \n\n) in the API response causes an EOF while parsing a value error. The change ensures that leading and trailing whitespace in choice_snapshot.message.content is stripped before parsing.
The updated code modifies the following section in
openai/lib/streaming/chat/_completions.py
:This prevents the EOF error during streaming caused by improperly formatted responses.
Additional context & links
\n\n{"foo": "bar"}
instead of the expected{"foo": "bar"}.
This causes the from_json function to fail during parsing.Root Cause: The original code does not handle leading or trailing whitespace in choice_snapshot.message.content before evaluating or parsing it.
Solution: By adding .strip() to choice_snapshot.message.content, the whitespace is removed, ensuring proper parsing of the JSON response.
This fix has been tested locally and resolves the issue without introducing side effects.If there are any concerns about this change or its potential impact, please feel free to share them.
issue #2082