BoundaryML/baml · error

__class_getitem__ for streaming

Error message

__class_getitem__ for streaming

What it means

After obtaining StreamState, pythonize_strict parameterizes it via `StreamState[value_type]` using `__class_getitem__`. The expect panics if that call raises, meaning the generic subscription of the streaming state type failed in the embedded interpreter.

Source

Thrown at engine/language_client_python/src/types/function_results.rs:370

    }?;

    let value_with_possible_completion_state = if completion_state.display && allow_partials {
        let value_type = value_with_possible_checks.bind(py).get_type();

        // Prepare the properties dictionary
        let properties_dict = pyo3::types::PyDict::new(py);
        properties_dict.set_item("value", value_with_possible_checks)?;
        properties_dict.set_item("state", format!("{:?}", completion_state.state))?;

        // Prepare type parameters for StreamingState[...]
        let type_parameters_tuple = PyTuple::new(py, [value_type.as_ref()]).expect("PyTuple::new");

        let class_streaming_state_type_constructor = partial_cls_module
            .getattr("StreamState")
            .expect("getattr(StreamState)");
        let class_completion_state_type: Bound<'_, PyAny> = class_streaming_state_type_constructor
            .call_method1("__class_getitem__", (type_parameters_tuple,))
            .expect("__class_getitem__ for streaming");

        let streaming_state_instance = class_completion_state_type
            .call_method(model_validate_method, (properties_dict.clone(),), None)
            .expect(model_validate_method);

        Ok::<Py<PyAny>, PyErr>(streaming_state_instance.into())
    } else {
        Ok(value_with_possible_checks)
    }?;

    Ok(value_with_possible_completion_state)
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Upgrade baml-py to match the engine version
  2. Avoid exotic types in type_builder aliases used with streaming
  3. Reproduce without streaming to confirm it is partial-specific
  4. Report to BAML maintainers if versions match
Defensive patterns

Strategy: try-catch

Validate before calling

from baml_py import StreamState
StreamState[str]  # generic parameterization works

Try / catch

try:
    async for partial in stream:
        ...
except Exception as e:
    logger.error('StreamState parameterization failed: %s', e)
    result = await stream.get_final_response()

Prevention

When it happens

Trigger: Streaming call where the value's Python type is not usable as a generic parameter to StreamState (e.g. an unsupported/anonymous type), or a StreamState class that is not Generic.

Common situations: Mixed engine/client versions during streaming, or custom type_builder types whose Python representation cannot parameterize StreamState.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/80dec53bc5782313. Report an issue: GitHub.