unslothai/unsloth · info · ValueError

known block type handled by its typed model

Error message

known block type handled by its typed model

What it means

Internal sentinel raised by AnthropicUnknownBlock's type validator when a block whose type is in _KNOWN_ANTHROPIC_BLOCK_TYPES reaches the fallback model. The Union is ordered so known types (text/image/tool_use/tool_result) parse as their typed models first; only genuinely unknown types should land here. Seeing this message means the discriminators did not route a known block to its typed model — effectively an internal invariant, not a client error.

Source

Thrown at studio/backend/models/inference.py:2456

# Block types the converter translates explicitly. Anything else (thinking /
# redacted_thinking, a provider block a resumed session replays, or a future type)
# is accepted as an unknown block and dropped by the converter, rather than 400-ing
# the whole request on strict validation.
_KNOWN_ANTHROPIC_BLOCK_TYPES = frozenset({"text", "image", "tool_use", "tool_result"})


class AnthropicUnknownBlock(BaseModel):
    type: str
    model_config = {"extra": "allow"}

    @field_validator("type")
    @classmethod
    def _only_unknown_types(cls, v):
        # Known types parse as their typed models above (so a malformed known block
        # still fails cleanly); this fallback only catches the rest.
        if v in _KNOWN_ANTHROPIC_BLOCK_TYPES:
            raise ValueError("known block type handled by its typed model")
        return v


AnthropicContentBlock = Union[
    AnthropicTextBlock,
    AnthropicImageBlock,
    AnthropicToolUseBlock,
    AnthropicToolResultBlock,
    AnthropicUnknownBlock,
]


def _anthropic_content_to_system_text(content: Any) -> str:
    """Convert misplaced system message content into Anthropic system text."""
    if content is None:  # null content must not become the literal "None"
        return ""
    if isinstance(content, str):
        return content

View on GitHub (pinned to 203007d190)

Solutions

  1. Inspect the failing block JSON and fix the malformed known-type block (missing/incorrect fields).
  2. If you maintain the server, check that the typed block models still accept legitimate payloads after an upgrade.
  3. Do not attempt to send known-type blocks through the 'unknown block' escape hatch — the validator forbids it by design.

Example fix

// before
{"type": "text", "text": null}  // null text fails AnthropicTextBlock, falls through

// after
{"type": "text", "text": "hello"}
Defensive patterns

Strategy: try-catch

Validate before calling

KNOWN = {'text', 'image', 'tool_use', 'tool_result'}
def block_parses_as_known(block: dict) -> bool:
    # best-effort client mirror; full checking belongs to the server models
    return block.get('type') in KNOWN

Type guard

def is_unknown_block(block: dict) -> bool:
    return isinstance(block.get('type'), str) and block['type'] not in {
        'text', 'image', 'tool_use', 'tool_result'
    }

Try / catch

try:
    req = AnthropicMessagesRequest.model_validate_json(body)
except ValidationError:
    # inspect which block failed; fix the malformed known-type block
    log_and_return_400()

Prevention

When it happens

Trigger: A content block with type 'text'/'image'/etc. that is malformed enough to fail its typed model's parsing (so Pydantic tries the fallback) — for example a text block missing required fields. Under Pydantic smart union the fallback then also rejects it, collapsing to a clean validation error rather than a silent passthrough.

Common situations: Gateway/proxy tests that construct half-valid Anthropic blocks; upstream schema drift adding a required field to a known block model so previously valid blocks no longer match the typed model; fuzzing the messages endpoint with mutated block payloads.

Related errors


AI-assisted analysis of unslothai/unsloth@203007d190 (2026-08-15). Data as JSON: /api/errors/3259d0a0edd33824. Report an issue: GitHub.