Skip to content

Bug: Thinking blocks not preserved in Anthropic conversations with tool calls #1704

@habema

Description

@habema

Error Details:

litellm.BadRequestError: AnthropicException - b'{"type":"error","error":{"type":"invalid_request_error","message":"messages.1.content.0.type: Expected `thinking` or `redacted_thinking`, but found `tool_use`. When `thinking` is enabled, a final `assistant` message must start with a thinking block (preceeding the lastmost set of `tool_use` and `tool_result` blocks). We recommend you include thinking blocks from previous turns. To avoid this requirement, disable `thinking`. Please consult our documentation at https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking"}}'

Root cause:

The SDK drops reasoning items when converting conversation history to API messages, violating Anthropic's requirement that assistant messages with tool calls must start with thinking blocks.

Minimal repro:

import asyncio
from agents import Agent, ModelSettings, Runner, SQLiteSession, function_tool
from openai.types.shared.reasoning import Reasoning

@function_tool
def get_weather(location: str) -> str:
    return f"The weather in {location} is sunny and 72°F."

async def main():
    agent = Agent(
        name="test_agent",
        model="litellm/anthropic/claude-sonnet-4-20250514",
        model_settings=ModelSettings(reasoning=Reasoning(effort="medium")),
        tools=[get_weather],
        instructions="You are a helpful assistant. When you need to use tools, think through the problem first.",
    )
    
    session = SQLiteSession(session_id="test_thinking_blocks")
    
    # This fails with the error above
    result = await Runner.run(
        agent, "What's the weather like in San Francisco?", session=session
    )

if __name__ == "__main__":
    asyncio.run(main())

Relevant Docs:

LiteLLM, Anthropic

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions