Repository navigation
fix(api): Responses reasoning items are the next turn's reasoning, not something it said - #2628
Open
chaitanya0305 wants to merge 1 commit into
Conversation
…t something it said A client that sends reasoning items back (Codex does, every turn) had each one rendered as an assistant message whose content is the reasoning, and a following function_call merged into it. The model then reads its own reasoning as text it said aloud, and the prompt no longer matches what it generated (e.g. Qwen3's <think> block), so the prefix cache misses at that turn on every request. A reasoning item now starts the assistant turn that follows it, as reasoning_content, the way the Chat Completions and Claude adapters pass reasoning to the chat template. Signed-off-by: Chaitanya Deshpande <dcchaitanyadeshpande@gmail.com>
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.
Motivation
Codex and other Responses API clients send the previous turns back as input items, including
reasoningitems. The Responses adapter turned each reasoning item into its own assistant message, with the reasoning as itscontent, and thefunction_callthat follows was merged into that message. As a result:<think>\n...\n</think>\n\n<tool_call>..., but it was re-rendered as...\n<tool_call>.... The prefix cache therefore misses from that turn onward, on every request of an agent loop;messagebecame two assistant turns in a row.Repro: send this input to
/v1/responsesand render the converted messages withQwen/Qwen3-0.6B's chat template:[ {"type": "message", "role": "user", "content": "List the files."}, {"type": "reasoning", "summary": [{"type": "summary_text", "text": "I should call ls."}]}, {"type": "function_call", "call_id": "call_1", "name": "shell", "arguments": "{\"cmd\": \"ls\"}"}, {"type": "function_call_output", "call_id": "call_1", "output": "a.py b.py"} ]Before:
After:
Changes
reasoningitem now opens an assistant turn withcontent: ""andreasoning_contentset to the reasoning text. Consecutive reasoning items are concatenated.function_call(or other tool call item) joins that turn through the existing_append_tool_call.messagefills in the turn'scontentinstead of starting a new assistant turn.Why It Works
reasoning_contentis the field that the chat templates (Qwen3, GLM, DeepSeek, ...) and exo's own prompt encoders read for a turn's reasoning. The Chat Completions adapter (reasoning_content) and the Claude adapter (thinkingblocks) already pass reasoning that way. Responses requests now take the same path.Test Plan
Manual Testing
responses_request_to_text_generationand rendered it with theQwen/Qwen3-0.6Btokenizer's chat template; output before and after is shown above.Automated Testing
TestReasoningInputItemsinsrc/exo/api/tests/test_openai_responses_api.pycovers a reasoning item followed by a tool call and one followed by an assistant message. Both tests fail onmainand pass with this change.ruff check,ruff format --checkandbasedpyrightare clean on the changed files.