Skip to content

Add support for thinking blocks and redacted thinking blocks in Anthropic v1/messages API#15501

Merged
Sameerlite merged 1 commit intolitellm_staging_octfrom
litellm_fix_anthropic_thinking_block_repair
Oct 13, 2025
Merged

Add support for thinking blocks and redacted thinking blocks in Anthropic v1/messages API#15501
Sameerlite merged 1 commit intolitellm_staging_octfrom
litellm_fix_anthropic_thinking_block_repair

Conversation

@Sameerlite
Copy link
Copy Markdown
Collaborator

Title

Add support for thinking blocks and redacted thinking blocks in Anthropic v1/messages API

Relevant issues

Fixes LIT-1260

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🆕 New Feature

Changes

This PR adds support for both regular thinking blocks and redacted thinking blocks in the Anthropic v1/messages API experimental pass-through adapter.

What was added:

  1. Type Definitions (litellm/types/llms/anthropic.py):

    • Added AnthropicResponseContentBlockThinking for regular thinking blocks
    • Added AnthropicResponseContentBlockRedactedThinking for redacted thinking blocks
    • Updated content union types to include both thinking block types
  2. Transformation Logic (litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py):

    • Enhanced translate_anthropic_messages_to_openai() to detect and convert thinking blocks from Anthropic's content array format to OpenAI's thinking_blocks field
    • Enhanced translate_openai_response_to_anthropic() to convert thinking blocks back to Anthropic's content array format
    • Added support for both thinking and redacted_thinking content types
    • Added proper imports for ChatCompletionThinkingBlock and ChatCompletionRedactedThinkingBlock
  3. Test Coverage (tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py):

    • Added test_translate_anthropic_messages_with_thinking_blocks_to_openai() for regular thinking blocks
    • Added test_translate_anthropic_messages_with_redacted_thinking_blocks_to_openai() for redacted thinking blocks
    • Both tests verify proper conversion and preservation of all fields

How it works:

Anthropic → OpenAI:

// Input (Anthropic)
{
  "role": "assistant", 
  "content": [
    {"type": "thinking", "thinking": "...", "signature": "..."},
    {"type": "text", "text": "Response"}
  ]
}

// Output (OpenAI)
{
  "role": "assistant",
  "content": "Response",
  "thinking_blocks": [
    {"type": "thinking", "thinking": "...", "signature": "..."}
  ]
}

OpenAI → Anthropic:

  • Converts thinking_blocks back to Anthropic's content array format
  • Supports both thinking and redacted_thinking types
  • Preserves all fields (thinking/data, signature, etc.)

This ensures that the v1/messages API properly retains thinking blocks as required in the OpenAI format while maintaining compatibility with Anthropic's native format.

Before:
Screenshot 2025-10-13 at 6 58 52 PM

After:
Screenshot 2025-10-13 at 6 57 49 PM

@vercel
Copy link
Copy Markdown

vercel bot commented Oct 13, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Ready Ready Preview Comment Oct 13, 2025 4:44pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant