Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Anthropics Anthropic sdk python ThinkingBlock Separation

From Leeroopedia
Knowledge Sources
Domains Extended_Thinking, LLM, Reasoning
Last Updated 2026-02-15 00:00 GMT

Overview

The Anthropic Python SDK implements content block separation through a set of Pydantic BaseModel classes, each representing a distinct content block type. For extended thinking, the three key types are ThinkingBlock, TextBlock, and RedactedThinkingBlock. These are combined into a ContentBlock discriminated union type alias.

ThinkingBlock

Source: src/anthropic/types/thinking_block.py (Lines 10-15)

class ThinkingBlock(BaseModel):
    signature: str

    thinking: str

    type: Literal["thinking"]

Import:

from anthropic.types import ThinkingBlock

Fields:

Field Type Description
thinking str The model's internal chain-of-thought reasoning text. Contains the full reasoning trace produced during the thinking phase.
signature str A cryptographic signature that verifies the integrity of the thinking block. Must be preserved exactly when passing this block back in multi-turn conversations.
type Literal["thinking"] Discriminator field identifying this as a thinking block.

TextBlock

Source: src/anthropic/types/text_block.py (Lines 12-23)

class TextBlock(BaseModel):
    citations: Optional[List[TextCitation]] = None
    """Citations supporting the text block.

    The type of citation returned will depend on the type of document being cited.
    Citing a PDF results in `page_location`, plain text results in `char_location`,
    and content document results in `content_block_location`.
    """

    text: str

    type: Literal["text"]

Import:

from anthropic.types import TextBlock

Fields:

Field Type Description
text str The model's visible response text. This is the primary content to display to end users.
citations Optional[List[TextCitation]] Optional list of citations supporting the text. Defaults to None. Citation types vary by source document type.
type Literal["text"] Discriminator field identifying this as a text block.

RedactedThinkingBlock

Source: src/anthropic/types/redacted_thinking_block.py (Lines 10-13)

class RedactedThinkingBlock(BaseModel):
    data: str

    type: Literal["redacted_thinking"]

Import:

from anthropic.types import RedactedThinkingBlock

Fields:

Field Type Description
data str Opaque encrypted data representing safety-filtered thinking content. The original reasoning text is not accessible. This data must be preserved exactly when passing it back for multi-turn conversations.
type Literal["redacted_thinking"] Discriminator field identifying this as a redacted thinking block.

ContentBlock Union

Source: src/anthropic/types/content_block.py (Lines 16-19)

ContentBlock: TypeAlias = Annotated[
    Union[TextBlock, ThinkingBlock, RedactedThinkingBlock, ToolUseBlock, ServerToolUseBlock, WebSearchToolResultBlock],
    PropertyInfo(discriminator="type"),
]

The ContentBlock union includes all six possible content block types. The PropertyInfo(discriminator="type") annotation tells the SDK's deserialization layer to use the type field to determine which class to instantiate.

Usage Example

Processing Thinking and Text Blocks

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=16000,
    thinking={"type": "enabled", "budget_tokens": 10000},
    messages=[{"role": "user", "content": "Solve this step by step: What is 15% of 340?"}]
)

for block in message.content:
    if block.type == "thinking":
        print(f"Thinking: {block.thinking}")
        print(f"Signature: {block.signature[:20]}...")
    elif block.type == "redacted_thinking":
        print(f"[Redacted thinking block - data length: {len(block.data)}]")
    elif block.type == "text":
        print(f"Answer: {block.text}")

Type-Safe Block Handling

from anthropic.types import ThinkingBlock, TextBlock, RedactedThinkingBlock

for block in message.content:
    if isinstance(block, ThinkingBlock):
        # Access thinking-specific fields
        reasoning = block.thinking
        sig = block.signature
    elif isinstance(block, RedactedThinkingBlock):
        # Preserve opaque data for multi-turn
        opaque_data = block.data
    elif isinstance(block, TextBlock):
        # Access text-specific fields
        answer = block.text
        cites = block.citations

Dependencies

  • pydantic.BaseModel: All block types inherit from the SDK's BaseModel (which wraps Pydantic's BaseModel).
  • typing_extensions.Literal: Used for the discriminator type field in each block type.
  • typing.Union: Used to define the ContentBlock union type.
  • typing_extensions.Annotated: Used to attach PropertyInfo discriminator metadata to the union.
  • anthropic._utils.PropertyInfo: Provides the discriminator annotation for efficient deserialization.

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment