BoundaryML/baml · error
__class_getitem__
Error message
__class_getitem__
What it means
During pythonize_strict, BAML wraps values that have checks in a Python `Checked[...]` generic type by calling `__class_getitem__` on the Checked class from the baml runtime module. The `.expect("__class_getitem__")` panics when that call fails — typically because the Checked generic or the typing parameters could not be constructed in the embedded Python interpreter.
Solutions
- Reinstall matching baml Python runtime (pip install -U baml) so the Checked generic supports parameterization
- Verify the Python interpreter used by BAML imports baml.types correctly (python -c "from baml_py import ...")
- Upgrade the BAML CLI and language client to the same version
- Report to BAML maintainers if versions match — this expect() should be a PyErr
Example fix
null
Defensive patterns
Strategy: try-catch
Validate before calling
import baml_py
assert hasattr(baml_py, 'Checked')
from baml_py import Checked
Checked[(int, __import__('typing').Literal['a'])] # parameterization works Type guard
def checked_param_ok(cls):
try:
cls[(int, __import__('typing').Literal['x'])]
return True
except TypeError:
return False Try / catch
try:
parsed = client.cast_to(result, MyType)
except Exception as e:
logger.exception('BAML checked-type conversion failed: %s', e) Prevention
- Keep baml-py and the BAML engine on identical versions
- Test checked/assertion features in CI after every upgrade
- Recreate the venv when upgrading BAML
When it happens
Trigger: Calling parse_llm_response or cast_to on a result whose value carries check annotations (checked blocks/assertions), when the Python `Checked` class does not support `__class_getitem__` or the type-parameters tuple contains invalid items.
Common situations: Mismatch between the Rust FFI and the installed baml-py Python package (stale or mixed versions after an upgrade), or a broken embedded Python environment where `typing.Literal` parameterization fails.
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
- __class_getitem__ for streaming
- getattr(StreamState)
- Internal error
- Internal error
- nil success in InvocationResponse
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/2aefbc01abf84a29.
Report an issue: GitHub.
Appendix: source
Thrown at engine/language_client_python/src/types/function_results.rs:335
// Collect check names as &str and turn them into a Python tuple
let check_names: Vec<&str> = checks.iter().map(|check| check.name.as_str()).collect();
let literal_args = PyTuple::new(py, check_names)?;
// Call Literal[...] dynamically
let literal_check_names = literal.get_item(literal_args).expect("get_item");
let class_checked_type_constructor =
cls_module.getattr("Checked").expect("getattr(Checked)");
// Prepare type parameters for Checked[...]
let type_parameters_tuple =
PyTuple::new(py, [value_type.as_ref(), &literal_check_names]).expect("PyTuple::new");
// Create the Checked type using __class_getitem__
let class_checked_type: Bound<'_, PyAny> = class_checked_type_constructor
.call_method1("__class_getitem__", (type_parameters_tuple,))
.expect("__class_getitem__");
// Prepare the properties dictionary
let properties_dict = pyo3::types::PyDict::new(py);
properties_dict.set_item("value", py_value_without_constraints)?;
if !checks.is_empty() {
properties_dict.set_item("checks", python_checks)?;
}
// Validate the model with the constructed type
let checked_instance = class_checked_type
.call_method(model_validate_method, (properties_dict.clone(),), None)
.expect(model_validate_method);
Ok::<Py<PyAny>, PyErr>(checked_instance.into())
} else {
Ok(py_value_without_constraints)
}?;
View on GitHub (pinned to bd85ce9dee)