Skip to content

fix(chat): parse structured output when reasoning returns chunked content - #621

Open
Nelson-PROIA wants to merge 1 commit into
mainfrom
nelson.proia/fix-parse-reasoning
Open

fix(chat): parse structured output when reasoning returns chunked content#621
Nelson-PROIA wants to merge 1 commit into
mainfrom
nelson.proia/fix-parse-reasoning

Conversation

@Nelson-PROIA

Copy link
Copy Markdown
Contributor

Problem

client.chat.parse() and parse_async() raise TypeError: 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:

"content": [
  {"type": "thinking", "thinking": [...]},
  {"type": "text", "text": "{...final JSON...}"}
]

convert_to_parsed_chat_completion_response in src/mistralai/extra/struct_chat.py only handled the str and None cases, so the chunked shape hit the else branch and raised.

Fix

When content is a list, concatenate the text of the TextChunk entries (ignoring ThinkChunk and 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, parsed is set to None, mirroring the existing content is None behavior.

Tests

Two regression tests in src/mistralai/extra/tests/test_struct_chat.py:

  • chunked content with thinking + text chunks parses into the pydantic model
  • content with only thinking chunks yields parsed=None

Full local verification: unittest discover on src/mistralai/extra/tests (149 tests OK), pytest tests/ (358 passed, 46 skipped), plus mypy, pyright, and ruff on src/mistralai/extra/

…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
Nelson-PROIA marked this pull request as ready for review September 11, 2026 11:57
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