Skip to content

fix: handle malformed JSON in structChat parsed response - #200

Closed
JacobiusMakes wants to merge 1 commit into
mistralai:mainfrom
JacobiusMakes:fix/structchat-json-parse-error-handling
Closed

JacobiusMakes wants to merge 1 commit into
mistralai: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>
@Nelson-PROIA

Copy link
Copy Markdown
Contributor

Thanks for the fix and the tests. Opened as a clean cherry-pick in #230 to land in the current release. Crediting you in the commit author and body.

Nelson-PROIA added a commit that referenced this pull request May 22, 2026
JSON.parse in convertToParsedChatCompletionResponse can throw a
SyntaxError when the model returns malformed JSON (common when
finish_reason is "length" and the output is truncated). Wrap the
parse in try/catch and leave parsed as undefined so callers can
detect the failure gracefully instead of crashing.

Also adds unit tests covering malformed JSON, valid JSON failing
schema validation, null content, empty choices, and undefined
choices.

Original work by @JacobiusMakes in #200.
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