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: handle whitespace-only content in structured output parsing #2216

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

valenradovich
Copy link

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

This commit fixes an issue where the API client would crash with JSON parsing errors when a model returns whitespace-only content (spaces, newlines, etc.) during structured output parsing.

Changes being requested

Changes:

  • Add a check in _parse_content to detect and gracefully handle empty or whitespace-only content before attempting JSON parsing
  • Improve streaming parser to skip JSON parsing for whitespace-only content
  • Update maybe_parse_content to catch and log parsing errors instead of letting them propagate
  • Add similar checks for tool argument parsing

This fixes cases where users were getting "EOF while parsing a value" errors when using client.beta.chat.completions.parse with models that occasionally return only whitespace instead of structured JSON.

With this change, parsing whitespace-only content now returns None for the parsed field instead of raising an exception, with an appropriate warning logged.

Additional context & links

Fixes this Issue entirely:
#1763

This commit fixes an issue where the API client would crash with JSON parsing
errors when a model returns whitespace-only content (spaces, newlines, etc.)
during structured output parsing.

Changes:
- Add a check in _parse_content to detect and gracefully handle empty or
  whitespace-only content before attempting JSON parsing
- Improve streaming parser to skip JSON parsing for whitespace-only content
- Update maybe_parse_content to catch and log parsing errors instead of
  letting them propagate
- Add similar checks for tool argument parsing

This fixes cases where users were getting "EOF while parsing a value" errors
when using client.beta.chat.completions.parse with models that occasionally
return only whitespace instead of structured JSON.

With this change, parsing whitespace-only content now returns None for the
parsed field instead of raising an exception, with an appropriate warning
logged.
@valenradovich valenradovich requested a review from a team as a code owner March 18, 2025 17:19
@sebipap
Copy link

sebipap commented Mar 18, 2025

I'm also having this issue, great work!
hope it get's merged

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.

2 participants