Repository navigation
feat(agent): keep the model's reasoning and send it back within a request - #4055
Conversation
…uest GAIA threw away almost everything a reasoning model wrote. The provider read reasoning_content only when a reply had no text (and then passed it off as the answer), and the loop deleted inline <think> blocks. On one DeepSeek call 91% of the output tokens were reasoning, so every step of a tool-calling job re-derived its plan from scratch. Reasoning now travels as its own field: the provider returns it apart from the answer, the transcript records it on each assistant step, and each step's reasoning goes back to the model as reasoning_content on its assistant message for the rest of the request. Reasoning stored from earlier user requests is not resent unless resend_reasoning_across_requests is set. Providers that reject the field (OpenAI, LiteLLM) never receive it.
Verdict: Request changesKeeping the model's reasoning as its own field instead of deleting it is the right call, and the layering (provider captures it, SDK only forwards it to backends that accept it, agent replays it within a request) is clean. Two edges in the parsing change need fixing before merge, and the required agent evals haven't been run. What needs attention:
One thing to confirm rather than change: the flag saying "this backend accepts reasoning in the history" is set for the whole local-inference provider, but that provider also points at any OpenAI-compatible URL a user configures. A strict server would reject the extra field on the second step of every tool-calling job — the same rejection the change is designed to avoid for other providers. Real-world evidenceN/A — no evidence bundle was produced for this run (no runnable-surface artifact present), so the verdict rests on static review plus reading the tests. The PR describes one real tool-call round trip through a Lemonade gateway to a DeepSeek model with 🔍 Technical detailsIssues🟡 Unclosed
The previous 🟡 Removing the
A narrow, non-silent recovery keeps the PR's invariant (reasoning is never the answer for a truncated reply) while not losing a complete one — the discriminator is 🟡 Agent evals required by
🟡
Nits🟢
🟢 Both copies of the dataclass are already missing 🟢 The unclosed-
Strengths
|
|
Verdict: Approve with suggestions The core change is well-motivated and the implementation is solid — reasoning is correctly captured from both 🟡 Required eval runs are unchecked in the test plan. 🟡 The streaming agent loop has no test coverage for the new reasoning path. The new test file sets 🔍 Technical detailsEval gap: Streaming test gap: if chunk_response.is_complete:
response_reasoning = getattr(chunk_response, "reasoning", None)and the plan streaming path ( Everything else looks correct: the |
|
Whichever of this PR and #4054 merges second needs one small follow-up. When a reply is cut off while the model is still thinking, its visible text is empty, so the continuation has to carry the reasoning or the model has nothing to continue from. 🔍 Technical detailsIn the cut-off branch of the loop, replace if response or reasoning:
messages.append(
{"role": "assistant", "content": response}
| ({"reasoning_content": reasoning} if reasoning else {})
)Add a test that replays a cut-off reply with empty text and reasoning, and asserts the continuation carries |
A reply that merely mentioned `<think>` part-way through prose was cut at that point, and everything the model said after it was silently deleted as reasoning. Only a reply that *opens* with an unclosed block was ever cut off mid-thought, so the pattern is now anchored there. Also from review: - Restore the reasoning_content-as-answer recovery for llama.cpp builds that route a finished reply into that field, gated on finish_reason="stop" so a reply cut off by the token limit still is not shown as the answer. - Route BuilderAgent's history through a shared _history_for_request, so resend_reasoning_across_requests holds for every subclass rather than only the base loop. - Document `usage` and `reasoning` on AgentResponse; both were missing from the SDK reference and the spec. - Cover the streaming loop, which reads reasoning off the is_complete chunk only, and pin the mid-prose-mention regression.
# Conflicts: # src/gaia/agents/base/agent.py
…odes The trace tests stubbed the chat response with a bare MagicMock, so once this branch started carrying the model's reasoning forward, getattr(response, "reasoning", None) returned an auto-created MagicMock instead of None. That rode into the turn record as reasoning_content and json.dump refused it, failing all ten tests in the file. Priming reasoning=None on both stubs keeps them a faithful stand-in for a backend that returns no reasoning. Production code is untouched.
|
All nine red unit tests were one missing field in a test stub, not a problem with the change itself. Fixed and pushed; the file is green locally on 3.13. The trace tests hand the agent a bare 🔍 Technical details
Both stubs in Other |
# Conflicts: # docs/spec/agent-base.mdx # src/gaia/agents/base/agent.py # src/gaia/llm/base_client.py # src/gaia/llm/providers/lemonade.py
Three tests in tests/unit/agents/test_model_history.py broke once this branch met current main: the agent loop read `.reasoning` straight off the chat response, and the MagicMock stubs that drive most agent-loop tests answer that with another MagicMock. It rode into the request history as `reasoning_content`, where json.dumps cannot serialise it. The same shape was patched once already in test_trace_tool_schema.py by priming the stub. There are 14 such _stub_chat helpers under tests/unit/agents/, so fixing them one at a time is a treadmill -- and any of them not asserting on serialised messages would keep silently polluting the history. Normalise at the capture point instead: AgentResponse.reasoning is Optional[str], so anything that is not a non-empty string is no reasoning.
|
Resynced with main, and found one real bug in the process: this branch breaks three history tests that pass on main, because it was reading the model's reasoning off the chat response without checking it was text. Fixed, with the review's earlier findings re-verified as already closed. The two evals remain open and I could not run them here. The bugMost agent-loop tests drive the agent with a bare
🔍 Technical detailsThe failures ( The fix — one helper beside def _response_reasoning(response: Any) -> Optional[str]:
value = getattr(response, "reasoning", None)
return value if isinstance(value, str) and value else None
New tests ( Mutation check — reverting the helper body to the plain Merge. Two rounds (main moved mid-review). Conflicts in Earlier review findings, re-verified as already fixed on the branch:
Tests:
Open, and not mine to close:
|
…ning # Conflicts: # docs/sdk/sdks/chat.mdx # docs/spec/agent-sdk.mdx # src/gaia/agents/base/agent.py # src/gaia/chat/sdk.py # tests/unit/test_claude_provider.py
GAIA threw away almost everything a reasoning model wrote. On one DeepSeek call, 91% of the output tokens were reasoning and the visible answer was 18 characters. So at every step of a tool-calling job, the model had to re-derive its plan from scratch, and a plan that existed only in its reasoning never reached the next step. When a reply had no visible text, GAIA did something worse: it handed the private reasoning to the user as the answer. Now reasoning is kept as its own field. It is recorded in the transcript, sent back to the model on each step of the same request (the provider's own guidance for thinking models that use tools), and never shown as the answer.
resend_reasoning_across_requests=Trueturns that on. The owner may want it on.fireworks.deepseek-v4p1-flash, in a tool-call loop, sent back an assistant turn carryingreasoning_content. It was accepted and the model answered coherently. The prompt grew by 27 tokens for 117 characters of reasoning, which shows the reasoning reaches the model rather than being dropped.🔍 Technical details
LLMClient.get_last_reasoning()andaccepts_reasoning_historyare added, andLemonadeProviderimplements both. The SDK attachesreasoning_contentto assistant history only for a provider that accepts it, so OpenAI and LiteLLM never receive the field, and Claude's message translation already drops unknown keys.<think>blocks go into the same field asreasoning_content, including an unclosed block (a reply cut off mid-thought) and a lone</think>(the chat template opened the block in the prompt).thinkingblocks it gets back, so it would have the same gap if thinking were enabled. That provider is not changed here.conversation_historyfrom the UI database (ui/_chat_helpers.py,ui/agent_loop.py) store only the answer text, so the setting has no effect in the Agent UI until they store reasoning too.Test plan
pytest tests/unit/agents/test_model_reasoning_kept.py. Reasoning fromreasoning_contentand from inline<think>blocks is stored and kept out of the answer. Within a request, the next call carries the previous step's reasoning. Across requests it is not resent by default and is resent with the setting on. Ten of these tests failed before the change.pytest tests/unit/agents tests/unit/chat tests/unit/test_claude_provider.py tests/unit/test_streaming_tool_calls.py tests/unit/test_tool_call_priority.pyreasoning_contentsent back (see above)gaia eval agent --category tool_selectiongaia eval agent --category rag_quality