Skip to content
TheChatArchiveThe Chat Archive
x-ai/grok-4.5· unknownHugging 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. Assistant

    I'll start by exploring the repository structure and locating the deferred capability / DeepSeek reasoningContent handling code.

  3. Assistant

    I'll search the codebase for deferred loading, reasoning_content, and DeepSeek handling.

  4. Assistant

    rg is not available, let me use grep.

  5. Assistant

    I'll look at how OpenAI/DeepSeek message history serializes thinking and reasoning_content.

  6. Assistant

    Let me look specifically at OpenAI model and DeepSeek profiles for reasoning_content.

  7. Assistant

    Next I'll inspect deferred capability loading and how it rewrites or filters messages.

  8. Assistant

    I'll open the deferred capability loader and OpenAI thinking mapping next.

  9. Assistant

    I'll read the OpenAI thinking mapping and check whether message history is stripped of ThinkingParts.

  10. Assistant

    I'll check recent history around deferred loading and thinking, plus DeepSeek profile settings.

  11. Assistant

    I'll inspect DeepSeek's profile and the deferred capability toolset next.

  12. Assistant

    I'll check tests and code about reasoning_content with tool calls, and recent DeepSeek/thinking fixes.

  13. Assistant

    I'll look specifically at message construction after load_capability and any filtering of ThinkingParts.

  14. Assistant

    _clean_message_history looks promising — I'll inspect that and related history cleanup.

  15. Assistant

    Let me look at _clean_message_history specifically.

  16. Assistant

    I'll check how DeepSeek provider constructs OpenAI profiles and how assistant messages with tools map thinking.

  17. Assistant

    I'll inspect ThinkingPart definition and multi-message mapping when tool results are present.

  18. Assistant

    I'll inspect LoadCapability return handling and how tool results reshape the message history.

  19. Assistant

    I'll look for related issues updates, CHANGELOG notes, and how tool returns interact with thinking.

  20. Assistant

    I'll search GitHub / the repo for the reported issue and related PRs.

  21. Assistant

    I'll study deferred capability tests and how FunctionModel could reproduce the history flow.

  22. Assistant

    I'll re-read DeepSeek reasoning docs and huddle patterns around multi-turn tool use.

  23. Assistant

    Checking when Thinking is auto-injected, and what happens to thinking model settings around deferred tools.

  24. Assistant

    I'll write a quick mock-based reproduction to inspect the second OpenAI request payload after load_capability.

  25. Assistant

    I'll build a temporary mock script that captures outbound OpenAI requests after deferred capability loading.

  26. Assistant

    The synthetic search_tools assistant message is missing reasoning_content. I'll find where that auto-load message is injected.

  27. Assistant

    I'll open the tool-search auto-load path next.

  28. Assistant

    I'll check similar handling elsewhere and DeepSeek tests covering multi-turn tool reasoning.

  29. Assistant

    I'll fetch DeepSeek's multi-turn reasoning guidance for the expected payload shape.

  30. Assistant

    I'll implement ensuring assistant messages include the thinking field when any sibling message already has it.

  31. Assistant

    Let me look at the _merge_leading_system_messages to place a similar helper nearby.

  32. Assistant

    I'll add a helper to backfill thinkingfields and call it from _map_messages.

  33. Assistant

    I'll wire the helper into _map_messages and fix the docstring typo.

  34. Assistant

    The second replace needed the right line range — fixing _map_messages next.

  35. Assistant

    Now I can do the replace.

  36. Assistant

    I'll re-run the reproduction to confirm yourselves synthetic messages now include reasoning_content.

  37. Assistant

    I'll run the OpenAI thinking mapping and DeepSeek-related tests next.

  38. Assistant

    Checking whether subclasses override _map_messages and may skip the new ensuring eagerness.

  39. Assistant

    Checking OpenRouter's _map_messages for the same failure mode.

  40. Assistant

    Checking xAI mapping — it may have the same reasoning_content requirement.

  41. Assistant

    I'll unit-test the helper for edge cases and clean up the temp script afterwards.

  42. Assistant

    The first call mutated the message list — I'll re-run the edge-case checks with fresh inputs.

  43. Assistant

    I'll review the final diff and run one more related OpenAI test sweep.

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