fix(chat): parse structured output when reasoning returns chunked content - #621
Open
Nelson-PROIA wants to merge 1 commit into
Open
fix(chat): parse structured output when reasoning returns chunked content#621Nelson-PROIA wants to merge 1 commit into
Nelson-PROIA wants to merge 1 commit into
Conversation
…tent With reasoning_effort enabled, the API returns message content as a list of chunks [ThinkChunk, TextChunk] instead of a plain JSON string. convert_to_parsed_chat_completion_response only handled str and None, so parse()/parse_async() raised TypeError. Concatenate the text chunks and parse the result. Content with only thinking chunks yields parsed=None.
Nelson-PROIA
marked this pull request as ready for review
September 11, 2026 11:57
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
client.chat.parse()andparse_async()raiseTypeError: Unexpected type for message.content: <class 'list'>whenever the response was produced with reasoning enabled, on any reasoning-capable model.With reasoning enabled the API returns the assistant content as a chunked list rather than a plain JSON string:
convert_to_parsed_chat_completion_responseinsrc/mistralai/extra/struct_chat.pyonly handled thestrandNonecases, so the chunked shape hit theelsebranch and raised.Fix
When
contentis a list, concatenate the text of theTextChunkentries (ignoringThinkChunkand any other chunk types) and validate that JSON against the caller's pydantic model, exactly like the string path. If the content contains no text chunks at all,parsedis set toNone, mirroring the existingcontent is Nonebehavior.Tests
Two regression tests in
src/mistralai/extra/tests/test_struct_chat.py:parsed=NoneFull local verification:
unittest discoveronsrc/mistralai/extra/tests(149 tests OK),pytest tests/(358 passed, 46 skipped), plusmypy,pyright, andruffonsrc/mistralai/extra/