Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parsing): ignore leading whitespace in message content #2083

Closed
wants to merge 2 commits into from

Conversation

blingblin-g
Copy link

@blingblin-g blingblin-g commented Feb 4, 2025

  • I understand that this repository is auto-generated and my pull request may not be merged

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 :

if (
    choice_snapshot.message.content.strip()
    and not choice_snapshot.message.refusal
    and is_given(self._rich_response_format)
):
    choice_snapshot.message.parsed = from_json(
        bytes(choice_snapshot.message.content, "utf-8"),
        partial_mode=True,
    )

This prevents the EOF error during streaming caused by improperly formatted responses.

Additional context & links

  • Problem: The API sometimes returns structured JSON responses with unexpected leading whitespace, such as \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

@RobertCraigie RobertCraigie changed the title fix(internal): handle leading whitespace in message content fix(parsing): ignore leading whitespace in message content Feb 10, 2025
Copy link
Collaborator

@RobertCraigie RobertCraigie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR but you'll also need to keep the original if choice_snapshot.message.content check in case the .content is None.

@blingblin-g
Copy link
Author

@RobertCraigie I think you’re right.
When I looked into modifying the code, it turned out to be quite similar to #2007, so I think we can close this PR.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants