Implementation:Anthropics Anthropic sdk python BetaRawContentBlockStartEvent
| Knowledge Sources | |
|---|---|
| Domains | API_Types, Beta_Features |
| Last Updated | 2026-02-15 12:00 GMT |
Overview
BetaRawContentBlockStartEvent is a Pydantic model representing the streaming event emitted when a new content block begins during a beta API streaming response. It carries the initial state of the content block along with its positional index.
Description
BetaRawContentBlockStartEvent extends BaseModel and is emitted as a Server-Sent Event (SSE) during streaming responses from the beta Messages API. It signals the start of a new content block and provides the full initial block object.
The file also defines a local ContentBlock type alias, which is a discriminated union identical to BetaContentBlock, covering all 15 beta content block types. This union uses PropertyInfo(discriminator="type") for automatic deserialization based on the type field.
Key fields:
- content_block -- The full initial content block object (one of the 15 block types)
- index -- The zero-based position of this block within the message's content array
- type -- Always
"content_block_start"
Usage
Use BetaRawContentBlockStartEvent when processing raw streaming events from the beta API. This event tells you that a new content block has begun and provides its initial state. You typically listen for this event to initialize tracking of a new block, then accumulate updates via subsequent delta events.
Code Reference
Source Location
- Repository: Anthropic SDK Python
- File:
src/anthropic/types/beta/beta_raw_content_block_start_event.py
Signature
ContentBlock: TypeAlias = Annotated[
Union[
BetaTextBlock,
BetaThinkingBlock,
BetaRedactedThinkingBlock,
BetaToolUseBlock,
BetaServerToolUseBlock,
BetaWebSearchToolResultBlock,
BetaWebFetchToolResultBlock,
BetaCodeExecutionToolResultBlock,
BetaBashCodeExecutionToolResultBlock,
BetaTextEditorCodeExecutionToolResultBlock,
BetaToolSearchToolResultBlock,
BetaMCPToolUseBlock,
BetaMCPToolResultBlock,
BetaContainerUploadBlock,
BetaCompactionBlock,
],
PropertyInfo(discriminator="type"),
]
class BetaRawContentBlockStartEvent(BaseModel):
content_block: ContentBlock
index: int
type: Literal["content_block_start"]
Import
from anthropic.types.beta import BetaRawContentBlockStartEvent
I/O Contract
| Field | Type | Required | Description |
|---|---|---|---|
content_block |
ContentBlock (discriminated union) |
Yes | The initial content block object; uses the same 15-variant union as BetaContentBlock
|
index |
int |
Yes | Zero-based position of this block in the message content array |
type |
Literal["content_block_start"] |
Yes | Always "content_block_start"
|
ContentBlock Variants
| Variant Type | type Value
|
|---|---|
BetaTextBlock |
"text"
|
BetaThinkingBlock |
"thinking"
|
BetaRedactedThinkingBlock |
"redacted_thinking"
|
BetaToolUseBlock |
"tool_use"
|
BetaServerToolUseBlock |
"server_tool_use"
|
BetaWebSearchToolResultBlock |
"web_search_tool_result"
|
BetaWebFetchToolResultBlock |
"web_fetch_tool_result"
|
BetaCodeExecutionToolResultBlock |
"code_execution_tool_result"
|
BetaBashCodeExecutionToolResultBlock |
"bash_code_execution_tool_result"
|
BetaTextEditorCodeExecutionToolResultBlock |
"text_editor_code_execution_tool_result"
|
BetaToolSearchToolResultBlock |
"tool_search_tool_result"
|
BetaMCPToolUseBlock |
"mcp_tool_use"
|
BetaMCPToolResultBlock |
"mcp_tool_result"
|
BetaContainerUploadBlock |
"container_upload"
|
BetaCompactionBlock |
"compaction"
|
Usage Examples
Processing Stream Events
import anthropic
client = anthropic.Anthropic()
with client.beta.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a haiku."}],
betas=["beta-feature-flag"],
) as stream:
for event in stream:
if event.type == "content_block_start":
print(f"Block {event.index} started: {event.content_block.type}")
if event.content_block.type == "text":
print(f" Initial text: '{event.content_block.text}'")
Type-Safe Block Handling
from anthropic.types.beta import BetaRawContentBlockStartEvent
def handle_block_start(event: BetaRawContentBlockStartEvent) -> None:
block = event.content_block
if block.type == "tool_use":
print(f"Tool use started at index {event.index}: {block.name}")
elif block.type == "thinking":
print(f"Thinking block started at index {event.index}")
elif block.type == "text":
print(f"Text block started at index {event.index}")
Related Pages
- Anthropics_Anthropic_sdk_python_BetaContentBlock -- The response-level union type matching the local
ContentBlockalias - Anthropics_Anthropic_sdk_python_BetaRawMessageDeltaEvent -- Message-level delta events emitted during streaming
- Anthropics_Anthropic_sdk_python_BetaMessage -- The complete message model assembled from streaming events
- Anthropics_Anthropic_sdk_python_Stream_Event_Processing -- General stream event processing patterns