Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Anthropics Anthropic sdk python BetaRawMessageDeltaEvent

From Leeroopedia
Revision as of 12:01, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Anthropics_Anthropic_sdk_python_BetaRawMessageDeltaEvent.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains API_Types, Beta_Features
Last Updated 2026-02-15 12:00 GMT

Overview

BetaRawMessageDeltaEvent is a Pydantic model representing a message-level delta event emitted during beta API streaming responses. It provides incremental updates about message-level state changes such as stop reason, usage statistics, container information, and context management.

Description

BetaRawMessageDeltaEvent extends BaseModel and is emitted as a Server-Sent Event (SSE) near the end of a streaming response. While content block delta events carry incremental text or tool input, this event carries message-level metadata updates.

The file defines two classes:

  • Delta -- An inner model containing the actual delta fields: stop_reason, stop_sequence, and container
  • BetaRawMessageDeltaEvent -- The outer event model with delta, usage, type, and context_management

Key fields:

  • delta -- Contains the stop reason, stop sequence, and optional container metadata
  • usage -- A BetaMessageDeltaUsage object with output token count at this point in the stream
  • context_management -- Optional information about context management strategies applied
  • type -- Always "message_delta"

Usage

Use BetaRawMessageDeltaEvent when processing raw streaming events from the beta API to detect when and why the model stopped generating. This event is typically one of the last events in a stream and carries the final stop reason, any matched stop sequence, and the cumulative usage statistics.

Code Reference

Source Location

Signature

class Delta(BaseModel):
    container: Optional[BetaContainer] = None
    stop_reason: Optional[BetaStopReason] = None
    stop_sequence: Optional[str] = None


class BetaRawMessageDeltaEvent(BaseModel):
    context_management: Optional[BetaContextManagementResponse] = None
    delta: Delta
    type: Literal["message_delta"]
    usage: BetaMessageDeltaUsage

Import

from anthropic.types.beta import BetaRawMessageDeltaEvent

I/O Contract

BetaRawMessageDeltaEvent Fields

Field Type Required Default Description
context_management Optional[BetaContextManagementResponse] No None Information about context management strategies applied
delta Delta Yes -- The delta payload with stop reason, stop sequence, and container
type Literal["message_delta"] Yes -- Always "message_delta"
usage BetaMessageDeltaUsage Yes -- Cumulative output token usage at this point in the stream

Delta Fields

Field Type Required Default Description
container Optional[BetaContainer] No None Container info for code execution tool
stop_reason Optional[BetaStopReason] No None Why the model stopped generating
stop_sequence Optional[str] No None Which custom stop sequence was matched

Usage Examples

Detecting Stop Reason in a Stream

import anthropic

client = anthropic.Anthropic()

with client.beta.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Tell me a story."}],
    betas=["beta-feature-flag"],
) as stream:
    for event in stream:
        if event.type == "message_delta":
            print(f"Stop reason: {event.delta.stop_reason}")
            print(f"Output tokens: {event.usage.output_tokens}")

Tracking Cumulative Usage

from anthropic.types.beta import BetaRawMessageDeltaEvent

def on_message_delta(event: BetaRawMessageDeltaEvent) -> None:
    if event.delta.stop_reason:
        print(f"Generation stopped: {event.delta.stop_reason}")
    if event.delta.stop_sequence:
        print(f"Matched stop sequence: {event.delta.stop_sequence}")
    print(f"Total output tokens so far: {event.usage.output_tokens}")
    if event.context_management:
        print("Context management was applied during this request.")

Related Pages

Page Connections

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