{"record":{"id":"ab2704b7edfb8384","repo":"BoundaryML/baml","slug":"expected-typebuilder-got","errorCode":null,"errorMessage":"Expected TypeBuilder, got {}","messagePattern":"Expected TypeBuilder, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_cffi/src/ctypes/function_args_decode.rs","lineNumber":59,"sourceCode":"                .map(|r| match r {\n                    Ok(RawPtrType::Collector(c)) => Ok(c),\n                    Err(e) => Err(e),\n                    Ok(other) => Err(anyhow::anyhow!(\"Expected Collector, got {}\", other.name())),\n                })\n                .collect::<Result<Vec<_>, _>>()?;\n            if collectors.is_empty() {\n                None\n            } else {\n                Some(collectors)\n            }\n        };\n        let type_builder = from\n            .type_builder\n            .map(RawPtrType::decode)\n            .transpose()?\n            .map(|r| match r {\n                RawPtrType::TypeBuilder(t) => Ok(t),\n                other => Err(anyhow::anyhow!(\n                    \"Expected TypeBuilder, got {}\",\n                    other.name()\n                )),\n            })\n            .transpose()?;\n\n        let tags = from\n            .tags\n            .into_iter()\n            .map(|v| {\n                from_host_kv_to_baml_kv(v).and_then(|(k, v)| match v {\n                    BamlValue::String(s) => Ok((k, s)),\n                    _ => anyhow::bail!(\"Expected string value for tag key {}\", k),\n                })\n            })\n            .collect::<Result<_, _>>()?;\n\n        Ok(BamlFunctionArguments {","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_cffi/src/ctypes/function_args_decode.rs#L41-L77","documentation":"Thrown while decoding BamlFunctionArguments when the optional type_builder field decodes to a RawPtrType that is not a TypeBuilder. The message names the actual variant found, indicating the host passed a pointer to a different kind of object.","triggerScenarios":"Passing an object other than a TypeBuilder (e.g. a Collector or client pointer) in the type_builder argument of a BAML function call, or a stale registry handle decoding to the wrong variant.","commonSituations":"Swapped variables in host code, reusing cached object handles after the registry was rebuilt, or SDK/engine version mismatch changing pointer variant ordering.","solutions":["Ensure the object passed as type_builder was created with the client's TypeBuilder API","Rebuild/reacquire the TypeBuilder handle if the runtime may have recycled it, and avoid caching handles across client restarts","Upgrade the host bindings and engine together so RawPtrType variants stay in sync"],"exampleFix":"// before (host pseudocode)\nargs.type_builder = collector\n\n// after\ntb = client.create_type_builder()\nargs.type_builder = tb","handlingStrategy":"type-guard","validationCode":"# host side\nif type_builder is not None and not isinstance(type_builder, TypeBuilder):\n    raise TypeError(f\"type_builder must be a TypeBuilder, got {type(type_builder).__name__}\")","typeGuard":"function isTypeBuilder(obj) { return obj && obj.__baml_type === 'TypeBuilder'; }","tryCatchPattern":"try:\n    result = client.CallFunction(fn, args)\nexcept Exception as e:\n    if \"Expected TypeBuilder\" in str(e):\n        raise TypeError(\"The type_builder argument must be a TypeBuilder instance\") from e\n    raise","preventionTips":["Create the TypeBuilder via the documented client API immediately before use","Avoid caching TypeBuilder handles across client sessions","Keep host SDK and engine versions aligned"],"tags":["ffi","decode","type-mismatch","rust"],"backgroundTag":"type-mismatch","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}