Skip to content
TheChatArchiveThe Chat Archive
gpt-5.6-sol· openaiHugging 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

    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. Assistant

    I’ll first inspect the repository structure and search for capability deferral, DeepSeek reasoning serialization, and relevant tests. Then I’ll identify the loop transformation that loses model metadata, reproduce it with a local/mock test if external API access is unavailable, patch it, and run focused tests plus broader checks.

  3. Assistant

    rg is unavailable, so I’ll use recursive grep and narrow the results to source and test files.

  4. Assistant

    The provider profile already opts DeepSeek V4 into sending thinking parts, so the likely defect is not basic OpenAI serialization but a graph-level message rewrite during local capability discovery. I’ll inspect that rewrite and the request/response mapping paths, along with capability-specific tests.

  5. Assistant

    That excerpt mixed the Responses API path with the relevant Chat Completions path. I’ll locate the chat mapper and inspect how ThinkingPart is grouped into assistant messages, then inspect deferred capability loading’s synthetic call/return handling to see whether it mutates the original response.

  6. Assistant

    I’ll now read the complete Chat Completions response normalization and mapping context. The key question is whether thinking is deliberately omitted when it lacks an ID, has a mismatched provider name, or is separated from the tool call by capability-part narrowing.

  7. Assistant

    The mapper itself will preserve any ThinkingPart in the ModelResponse, including after narrowing the load-capability call. I’ll inspect graph response processing for places that construct a replacement ModelResponse(parts=...); this is the most plausible point where only tool-call parts are retained for an intermediate turn.

  8. Assistant

    The graph appends the full response before executing tools, so normal capability loading should retain thinking. The narrowing function does replace parts and is invoked only on the raw handler response; I’ll inspect it because an incorrect discriminator may classify/drop non-tool parts. I’ll also inspect history cleanup, which can merge adjacent requests and responses.

  9. Assistant

    The test reveals an important extra turn transformation: after load_capability, local tool search synthesizes a ModelResponse(ToolSearchCallPart) and ModelRequest(ToolSearchReturnPart) into history. DeepSeek then sees two assistant/tool exchanges, and the synthetic assistant response has no reasoning_content. The API error may refer to the most recent tool-call assistant message requiring reasoning. I’ll inspect synthesis and provider preparation semantics.

  10. Assistant

    The synthetic exchange is indeed the problematic shape: it invents a second assistant tool-call message with no thinking block. For providers that require thinking replay, the safest coherent representation is to fold the synthetic search call into the preceding assistant response and its return into the existing tool-return request, making them parallel tool calls under the original, exact reasoning block. This avoids fabricating or duplicating reasoning. Before changing behavior globally, I’ll inspect tool-search history parsers and tests around persistence to ensure the folded shape remains supported.

  11. Assistant

    The parsers are part-based, so folding preserves persistence and discovery semantics. I’ll implement conditional folding only when the immediately preceding real response contains thinking: non-reasoning providers retain the current history shape, while reasoning providers keep the synthetic call under the response that supplied the exact thinking content. If the expected adjacent shape is absent, the existing append behavior remains as a safe fallback.

  12. Assistant

    The patch is syntactically clean. I’ll run the existing deferred capability end-to-end and persistence tests first; because they use non-thinking FunctionModel, they should verify no behavior regression in the existing path. Then I’ll create a temporary local reproduction that injects a ThinkingPart and captures the second-turn history, without adding repository tests.

  13. Assistant

    Existing behavior passes. For the reproduction, I’ll model the DeepSeek sequence locally: first response contains reasoning plus load_capability; on the next model invocation I’ll assert there is no synthetic assistant response lacking thinking, and that both the original load call and automatic search call share the exact original thinking block with both returns in the following request.

  14. Assistant

    The graph-level reproduction confirms the bad extra assistant message is eliminated and the exact reasoning remains attached. I’ll now verify the actual DeepSeek Chat Completions serializer maps this folded history to one assistant message containing reasoning_content and both tool calls, followed by both tool results. This checks the wire representation directly without requiring an API key/network call.

  15. Assistant

    The wire check matches DeepSeek’s requirement exactly. I’ll run formatting/linting and a broader subset of tool-search/deferred-capability tests. I’ll also review the diff for unintended changes and consider edge cases such as non-adjacent histories and multiple thinking parts.

  16. Assistant

    Lint passes, but the formatter requests a line wrap; because commands were chained, the broader tests did not run. I’ll apply the project formatter, rerun lint and the selected tests, then run the full capability test module if the focused set remains quick enough.

  17. Assistant

    The selected capability tests pass. I’ll run all capability tests to catch ordering/history interactions, plus the OpenAI model tests that cover reasoning_content and DeepSeek profiles. Before that, I’ll identify exact test names so the model suite can stay focused.

  18. Assistant

    All 720 capability tests pass. I’ll run the focused OpenAI thinking-field tests and DeepSeek provider tests, then run the repository’s static type checker on the changed package/file if its configured command is discoverable.

  19. Assistant

    Provider and serializer tests pass. I’ll inspect the Makefile target and run Pyright; then I’ll review the final diff. One edge case is an interrupted or transformed history where the latest request is not paired with a response—our explicit adjacency checks correctly retain the old append behavior there. Multiple thinking parts are preserved unchanged because the entire response is copied.

  20. Assistant

    The final review suggests tightening the fold condition: a loaded capability can also be reconstructed from older history, in which case the latest thinking response may be unrelated. I’ll require a matching load_capability call/return pair in the adjacent response/request before folding. This keeps the fix scoped precisely to the immediate deferred-load turn and preserves append-only replay behavior for legacy/incomplete histories.

  21. Assistant

    The tightened condition is formatted and lint-clean. I’ll rerun the local graph reproduction, the complete capability suite, focused provider/serializer tests, and Pyright once more so the final state—not the earlier version—is fully verified.

  22. Assistant

    The reproduction and all 733 tests passed, but Pyright found an ambiguous empty-set inference in the new guard. I’ll add an explicit set[str] annotation, then rerun Pyright and final checks.

  23. Assistant

    All final static checks pass, and only the intended source file is modified. I’ll do one last diff review to ensure the implementation remains concise and no temporary artifacts are present, then submit.

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