Skip to content

[Opus 4.6] BetaCompactionBlock deserialized as ParsedBetaTextBlock during streaming accumulation #1175

@Julien-Abulfeda

Description

@Julien-Abulfeda

SDK Version [anthropic==0.79.0] (Python 3.12) via vertex

Description

When using the beta streaming API with compaction=True, the BetaCompactionBlock in the final ParsedBetaMessage.content is incorrectly typed as a ParsedBetaTextBlock instead of BetaCompactionBlock.

Reproduction

kwargs["betas"]=["compact-2026-01-12"]
kwargs["context_management"]={
    "edits": [{
        "type": "compact_20260112",
        "trigger": {"type": "input_tokens", "value": 50000},
        "pause_after_compaction": pause_after_compaction
    }]}
client = AsyncAnthropicVertex(
    project_id=project_id,
    region=region,
    credentials=credentials  # <-- Fix: credentials passées directement
)
async with client.beta.messages.stream(**kwargs) as stream:
    message = await stream.get_final_message()
    
    for block in message.content:
        print(type(block), block.type)
        # Expected: <class 'BetaCompactionBlock'> compaction
        # Actual:   <class 'ParsedBetaTextBlock'> compaction

Observed behavior

When a compaction event occurs, the streaming events (content_block_start, content_block_delta and content_block_stop for "compaction" are correctly typed but the final message contains:

ParsedBetaTextBlock(citations=None, text=None, type='compaction', parsed_output=None, content=None)

  • type(block) is ParsedBetaTextBlock, not BetaCompactionBlock
  • The object has parasitic fields from ParsedBetaTextBlock (text, citations, parsed_output) all set to None
  • block.type is correctly "compaction"
  • Calling model_dump() / model_dump_json() on the parent message triggers Pydantic serialization warnings for every variant in the union

Expected behavior

message.content[i] should be an instance of BetaCompactionBlock when block.type == "compaction".

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions