Implementation:Anthropics Anthropic sdk python ThinkingBlock Separation
| 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'sBaseModel(which wraps Pydantic'sBaseModel).typing_extensions.Literal: Used for the discriminatortypefield in each block type.typing.Union: Used to define theContentBlockunion type.typing_extensions.Annotated: Used to attachPropertyInfodiscriminator metadata to the union.anthropic._utils.PropertyInfo: Provides thediscriminatorannotation for efficient deserialization.