{"record":{"id":"b9e7849d843e0e8b","repo":"BoundaryML/baml","slug":"expected-float-got","errorCode":null,"errorMessage":"Expected float, got {}","messagePattern":"Expected float, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2627,"sourceCode":"        use serde_json::Value as JsonValue;\n\n        match (json, target_type) {\n            (JsonValue::Null, _) => Ok(BamlValueWithMeta::Null(meta.clone())),\n            (JsonValue::Bool(b), TypeIR::Primitive(baml_types::TypeValue::Bool, _)) => {\n                Ok(BamlValueWithMeta::Bool(*b, meta.clone()))\n            }\n            (JsonValue::Number(n), TypeIR::Primitive(baml_types::TypeValue::Int, _)) => {\n                if let Some(i) = n.as_i64() {\n                    Ok(BamlValueWithMeta::Int(i, meta.clone()))\n                } else {\n                    bail!(\"Expected integer, got {}\", n)\n                }\n            }\n            (JsonValue::Number(n), TypeIR::Primitive(baml_types::TypeValue::Float, _)) => {\n                if let Some(f) = n.as_f64() {\n                    Ok(BamlValueWithMeta::Float(f, meta.clone()))\n                } else {\n                    bail!(\"Expected float, got {}\", n)\n                }\n            }\n            (JsonValue::String(s), TypeIR::Primitive(baml_types::TypeValue::String, _)) => {\n                Ok(BamlValueWithMeta::String(s.clone(), meta.clone()))\n            }\n            (JsonValue::Array(arr), TypeIR::List(elem_type, _)) => {\n                let mut baml_list = Vec::new();\n                for item in arr {\n                    baml_list.push(json_to_baml(item, elem_type, meta)?);\n                }\n                Ok(BamlValueWithMeta::List(baml_list, meta.clone()))\n            }\n            (JsonValue::Object(obj), TypeIR::Map(_, value_type, _)) => {\n                let mut baml_map = BamlMap::new();\n                for (key, value) in obj {\n                    baml_map.insert(key.clone(), json_to_baml(value, value_type, meta)?);\n                }\n                Ok(BamlValueWithMeta::Map(baml_map, meta.clone()))","sourceCodeStart":2609,"sourceCodeEnd":2645,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2609-L2645","documentation":"Thrown during JSON-to-BAML conversion when a JSON number cannot be converted to f64 for a Float target type. as_f64() fails essentially only for values outside f64's representable range or non-finite handling, since JSON numbers map to f64 otherwise. It enforces the declared BAML type rather than coercing.","triggerScenarios":"parse_json_to_baml_value / json_to_baml receives (Number, TypeIR::Float) where n.as_f64() returns None — practically, extremely large magnitudes like 1e400 or malformed arbitrary-precision numbers the JSON crate cannot represent as f64.","commonSituations":"LLM or upstream API emitting absurdly large numeric literals (1e999) into a float field; copy-pasted JSON with scientific notation beyond double range.","solutions":["Check the JSON payload at the failing path and correct/normalize the out-of-range number","Cap or clamp the value in the producer before conversion","If arbitrary precision is needed, type the field as string and parse manually downstream","Validate numeric ranges of upstream JSON before calling the BAML converter"],"exampleFix":"// before (JSON)\n{\"ratio\": 1e400}\n// after (JSON)\n{\"ratio\": 1.0}  // or send \"1e400\" as a string field","handlingStrategy":"validation","validationCode":"function ensureFloat(n: unknown): number {\n  if (typeof n !== 'number' || !Number.isFinite(n)) {\n    throw new Error(`Expected finite float, got ${n}`);\n  }\n  return n;\n}","typeGuard":null,"tryCatchPattern":"try {\n  value = parseJsonToBamlValue(json, schema);\n} catch (e) {\n  if (String(e).startsWith('Expected float')) {\n    // clamp or normalize the out-of-range number\n  }\n  throw e;\n}","preventionTips":["Clamp numbers to f64 range upstream","Reject non-finite magnitudes before conversion","Use string fields for extreme-precision values"],"tags":["baml","json","float","deserialization"],"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"}