Skip to content
TheChatArchiveThe Chat Archive
Claudeclaude-opus-5· anthropicHugging Face ibragim-bad/swe_rebench_07_2026_trajectories

ISSUE DESCRIPTION Deferred capabilities throw error 400 with Deepseek models Initial…

  1. User

    ISSUE DESCRIPTION

    Deferred capabilities throw error 400 with Deepseek models

    Initial Checks

    Description

    When running an agent using a DeepSeek reasoning model, using Capability(..., defer_loading=True) causes a ModelHTTPError: status_code: 400 during execution loop turns.

    openai.BadRequestError: Error code: 400 - {'error': {'message': 'The `reasoning_content` in the thinking mode must be passed back to the API.', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}
    

    This is triggered because DeepSeek requires the original reasoning_content produced during the thinking phase to be passed back exactly as-is in the subsequent conversation message history array. When defer_loading=True, the graph loop intercepted by PydanticAI's capability runner seems to drop or fail to serialize/propagate the reasoning_content token block back to DeepSeek's chat completions endpoint.

    Setting defer_loading=False solves the issue, and the agent executes successfully.

    Minimal, Reproducible Example

    # Remember to export the DEEPSEEK_API_KEY environment variable first, then run this code
    
    import random
    from pydantic_ai import Agent, RunContext
    from pydantic_ai.capabilities import Capability
    
    
    def roll_dice() -> str:
      """Roll a six-sided die and return the result."""
      return str(random.randint(1, 6))
    
    def get_player_name(ctx: RunContext[str]) -> str:
      """Get the player's name."""
      return ctx.deps
    
    
    agent = Agent(
      'deepseek:deepseek-v4-flash',  
      deps_type=str,  
      instructions=(
          "You're a dice game, you should roll the die and see if the number "
          "you get back matches the user's guess. If so, tell them they're a winner. "
          "Use the player's name in the response."
      ),
      capabilities=[Capability(id='DICE_ROLL', tools=[get_player_name, roll_dice], defer_loading=True)] # Change this to False and the agent will run successfully.
    )
    
    dice_result = agent.run_sync('My guess is 4', deps='Anne')  
    print(dice_result.output)
    

    Logfire Trace

    No response

    Python, Pydantic AI & LLM client version

    • Python: 3.14
    • Pydantic AI: 1.106.0
    • LLM provider SDK: deepseek

    Interface

    No new interfaces are introduced.

    ADDITIONAL ADVICE

    Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state. You can also reset all changes in the repository using git reset --hard command. Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands. However you don't need to use git add or git commit before submitting your changes. In fact, submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.

    CHECKLIST

    Before submitting your solution, please go over the following checklist and make sure you've done everything:

    • If an error was reported in the issue description, I have successfully reproduced it.
    • If an error was reported in the issue description, I have confirmed that my fix resolves the error.
    • I have thought about edge cases and made sure my fix handles them as well.
    • I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.

    Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!

    (Current directory: /pydantic-ai, current file: none) bash-$

  2. Claude Assistant

    Now let me write a repro script.

  3. Claude Assistant

    Now let me implement the fix.

  4. Claude Assistant

    Now let's run the repro to see the effect.

  5. Claude Assistant

    I've completed the fix. Here's my checklist review:

    1. If an error was reported in the issue description, I have successfully reproduced it. Yes. I wrote a repro script using the repo's MockOpenAI client with OpenAIChatModel('deepseek-reasoner', provider=DeepSeekProvider(...)), an agent with Capability(id='DICE_ROLL', tools=[roll_dice], defer_loading=True), and mocked responses carrying reasoning_content. Dumping the outgoing messages payload showed the root cause: after the model calls load_capability, ToolSearch.before_model_request appends a framework-synthesized ModelResponse(parts=[ToolSearchCallPart(...)]) (auto_load_* search_tools call). On the wire that becomes an assistant message with tool_calls and no reasoning_content, which is exactly what DeepSeek rejects with 400 - The 'reasoning_content' in the thinking mode must be passed back to the API. With defer_loading=False no such synthetic message exists — matching the issue report.

    2. If an error was reported in the issue description, I have confirmed that my fix resolves the error. Yes. In OpenAIChatModel._map_messages I now fold framework-synthesized, tool-call-only ModelResponses into the last real model response, but only when that response's thinking is actually echoed back in a dedicated field (DeepSeek/Moonshot-style reasoning_content). After the fix, the repro's payloads contain no thinking-less assistant message: the search_tools call rides on the assistant message that carries reasoning_content, and both tool returns follow it, so call/return pairing is intact. I also verified with a direct _map_messages comparison that a plain OpenAI model's wire shape is completely unchanged.

    3. I have thought about edge cases and made sure my fix handles them as well.

    • Only responses with no provider/model identity (provider_response_id/provider_name/model_name all None) and containing exclusively ToolCallParts are folded — real provider responses are never mangled.
    • Folding only happens when the target response has a ThinkingPart that this profile sends back in a field, so 'tags', False and plain-OpenAI histories go out byte-identical (no cache/behavior changes for other providers).
    • Cross-provider histories, 'auto' detection (id/provider/custom-field matching) and the 'field' mode are all handled via a shared helper _auto_thinking_field, which I refactored out of _map_response_thinking_part so both paths can't drift.
    • Message history (all_messages()) is untouched, so serialization, discovered_tool_names parsing and cross-provider replay keep working; the change is wire-only, applies to streaming and non-streaming (shared _map_messages) and is inherited by subclasses like OpenRouterModel that call super()._map_messages.

    4. I have run existing tests in the repository that might have been affected and confirmed that they pass. Yes. tests/models/test_openai.py, tests/models/test_deepseek.py, tests/models/test_openrouter.py, tests/models/test_ollama.py, tests/models/test_cerebras.py, tests/test_capabilities.py, tests/test_tool_search.py, tests/test_agent.py and the whole tests/models tree all pass. The full suite showed only pre-existing failures (a huggingface_hub deprecation warning in test_init/test_model_* and three MCP subprocess tests), which I confirmed fail identically on a stashed (clean) tree. ruff format --check, ruff check and pyright are clean on the modified file. No tests were modified or added.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.