openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

fix: handle whitespace-only content in structured output parsing

Open valenradovich opened this issue 9 months ago • 1 comments

  • [x] 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: https://github.com/openai/openai-python/issues/1763

valenradovich avatar Mar 18 '25 17:03 valenradovich

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

sebipap avatar Mar 18 '25 17:03 sebipap