BoundaryML/baml · error
Failed to coerce value
Error message
Failed to coerce value: {:?} What it means
In jsonish::from_str, after coercing the parsed value to the target type, the code checks the coercion flags: if the only evidence was an 'InferedObject' over a plain String (the parser guessed an object from a bare string), it refuses the result with 'Failed to coerce value'. This prevents accepting a string that was silently reinterpreted as the requested object type.
Solutions
- Make the prompt demand the full object shape with required fields
- Check the target type — if a scalar is acceptable, use a string/class field type instead
- Inspect the printed flags in the error to confirm the inferred-object coercion
- Retry the generation or fall back to manual parsing for this response
Example fix
// schema
// before: class Output { value string }
// model returned: "just a string"
// after: prompt: 'Respond with JSON: {"value": "..."}' Defensive patterns
Strategy: validation
Validate before calling
fn is_scalar_only(s: &str) -> bool { let t = s.trim().trim_matches(|c| c == '`'); !(t.starts_with('{') || t.starts_with('[')) } Try / catch
match jsonish::from_str(&object_type, out) {
Err(e) if e.to_string().contains("Failed to coerce value") => {
// schema expects an object but model returned a scalar
retry_with_schema_example()
}
other => other,
} Prevention
- Show the exact object schema in the prompt
- If a scalar is valid, type the field as string not class
- Feed one example output so the model mirrors the shape
When it happens
Trigger: from_str(&target_type, s) where the raw text is just a quoted/unquoted string but the target type is a class/object — the coercion produced an inferred object from a string flag.
Common situations: LLM returned a single value (e.g. a name or URL) where the schema expects an object; type changed in the BAML schema but the model output didn't.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- Arrow types are not supported in CFFI
- baml.json.deserialize failed
- ` ` is declared by the stdlib
- baml.json.serialize failed
- BamlValidationError
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/a7dbfd761ac28027.
Report an issue: GitHub.
Appendix: source
Thrown at engine/baml-lib/jsonish/src/lib.rs:267
of,
if raw_string_is_done {
baml_types::StreamingMode::NonStreaming
} else {
baml_types::StreamingMode::Streaming
},
);
// Determine the best way to get the desired schema from the parsed schema.
// Lets try to now coerce the value into the expected schema.
let parsed_value: BamlValueWithFlags = match target.coerce(&ctx, target, Some(&value)) {
Ok(v) => {
if v.conditions()
.flags()
.iter()
.any(|f| matches!(f, Flag::InferedObject(jsonish::Value::String(_, _))))
{
anyhow::bail!("Failed to coerce value: {:?}", v.conditions().flags());
}
Ok::<BamlValueWithFlags, anyhow::Error>(v)
}
Err(e) => anyhow::bail!("Failed to coerce value: {:?}", e),
}?;
log::debug!("Parsed JSONish (step 2 of parsing): {parsed_value:#?}");
// let value: BamlValue = parsed_value.clone().into();
// println!("from_str value: {value}");
// println!("-------------------------------------------------");
// parsed_value.clear_flags();
Ok(parsed_value)
}
impl ResponseBamlValue {View on GitHub (pinned to bd85ce9dee)