{"record":{"id":"2aefbc01abf84a29","repo":"BoundaryML/baml","slug":"class-getitem","errorCode":null,"errorMessage":"__class_getitem__","messagePattern":"__class_getitem__","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_python/src/types/function_results.rs","lineNumber":335,"sourceCode":"\n        // Collect check names as &str and turn them into a Python tuple\n        let check_names: Vec<&str> = checks.iter().map(|check| check.name.as_str()).collect();\n        let literal_args = PyTuple::new(py, check_names)?;\n\n        // Call Literal[...] dynamically\n        let literal_check_names = literal.get_item(literal_args).expect(\"get_item\");\n\n        let class_checked_type_constructor =\n            cls_module.getattr(\"Checked\").expect(\"getattr(Checked)\");\n\n        // Prepare type parameters for Checked[...]\n        let type_parameters_tuple =\n            PyTuple::new(py, [value_type.as_ref(), &literal_check_names]).expect(\"PyTuple::new\");\n\n        // Create the Checked type using __class_getitem__\n        let class_checked_type: Bound<'_, PyAny> = class_checked_type_constructor\n            .call_method1(\"__class_getitem__\", (type_parameters_tuple,))\n            .expect(\"__class_getitem__\");\n\n        // Prepare the properties dictionary\n        let properties_dict = pyo3::types::PyDict::new(py);\n        properties_dict.set_item(\"value\", py_value_without_constraints)?;\n        if !checks.is_empty() {\n            properties_dict.set_item(\"checks\", python_checks)?;\n        }\n\n        // Validate the model with the constructed type\n        let checked_instance = class_checked_type\n            .call_method(model_validate_method, (properties_dict.clone(),), None)\n            .expect(model_validate_method);\n\n        Ok::<Py<PyAny>, PyErr>(checked_instance.into())\n    } else {\n        Ok(py_value_without_constraints)\n    }?;\n","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_python/src/types/function_results.rs#L317-L353","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"null","handlingStrategy":"try-catch","validationCode":"import baml_py\nassert hasattr(baml_py, 'Checked')\nfrom baml_py import Checked\nChecked[(int, __import__('typing').Literal['a'])]  # parameterization works","typeGuard":"def checked_param_ok(cls):\n    try:\n        cls[(int, __import__('typing').Literal['x'])]\n        return True\n    except TypeError:\n        return False","tryCatchPattern":"try:\n    parsed = client.cast_to(result, MyType)\nexcept Exception as e:\n    logger.exception('BAML checked-type conversion failed: %s', e)","preventionTips":["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"],"tags":["python","ffi","internal"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}