Skip to content

fix: handle malformed JSON in structChat parsed response#200

Open
JacobiusMakes wants to merge 1 commit intomistralai:mainfrom
JacobiusMakes:fix/structchat-json-parse-error-handling
Open

fix: handle malformed JSON in structChat parsed response#200
JacobiusMakes wants to merge 1 commit intomistralai:mainfrom
JacobiusMakes:fix/structchat-json-parse-error-handling

Conversation

@JacobiusMakes
Copy link
Copy Markdown

Summary

convertToParsedChatCompletionResponse in src/extra/structChat.ts crashes with an unhandled SyntaxError when the model returns malformed JSON content (common when finish_reason is "length" due to token limit truncation).

Bug: The JSON.parse() call on line 127 is unguarded. While responseFormat.safeParse() correctly uses Zod's safe parsing, the preceding JSON.parse() throws on invalid JSON, propagating an uncaught exception to the caller.

Secondary issue: When a choice has null, undefined, or array content, the choice was silently dropped from the output (the inner if had no else branch), causing the response to have fewer choices than expected.

Changes

  • Wrap JSON.parse() in a try/catch so malformed JSON gracefully sets parsed to undefined instead of throwing
  • Add an else branch to preserve choices with non-string content (null/undefined/array) with parsed: undefined
  • Add 5 new test cases covering: malformed JSON, schema validation failure, null content, empty choices, undefined choices

Test plan

  • All 8 structChat tests pass (3 existing + 5 new)
  • Full test suite passes (39/39 tests)
  • Verify behavior with a real truncated response from the API

…nResponse

The unguarded JSON.parse() call in convertToParsedChatCompletionResponse
throws an unhandled SyntaxError when the model returns malformed JSON
(common when finish_reason is "length" due to token limit truncation).

This wraps the JSON.parse in a try/catch so that malformed content
gracefully sets parsed to undefined instead of crashing. Also fixes
a secondary issue where choices with null/undefined/array content were
silently dropped from the output instead of being preserved.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

1 participant