{"record":{"id":"eadb171d9ab640a5","repo":"BoundaryML/baml","slug":"expected-integer-got","errorCode":null,"errorMessage":"Expected integer, got {}","messagePattern":"Expected integer, got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/baml-compiler/src/thir/interpret.rs","lineNumber":2620,"sourceCode":"\n    fn json_to_baml(\n        json: &serde_json::Value,\n        target_type: &TypeIR,\n        meta: &ExprMetadata,\n    ) -> Result<BamlValueWithMeta<ExprMetadata>> {\n        use baml_types::TypeIR;\n        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()))","sourceCodeStart":2602,"sourceCodeEnd":2638,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/baml-compiler/src/thir/interpret.rs#L2602-L2638","documentation":"Thrown during JSON-to-BAML conversion when a JSON number cannot be represented as an i64 integer for a BamlValue::Int target. The value's declared BAML type is Int, but the JSON number is fractional (e.g. 3.14) or exceeds the i64 range. json_to_baml is strict: it will not silently truncate or wrap.","triggerScenarios":"Calling parse_json_to_baml_value / json_to_baml with a TypeIR of Int and JSON input like `1.5`, `1e30`, or a huge number beyond 2^63-1. Recursive calls via json_to_baml hit the same bail for nested fields.","commonSituations":"Feeding LLM-produced JSON with float values (e.g. `\"score\": 0.87`) into a schema declaring `int`, or passing timestamps in milliseconds as large numbers that overflow i64 in exotic formats.","solutions":["Change the declared BAML type of the field from int to float if fractional values are expected","Clamp/round the JSON value to an integer before feeding it to the converter (e.g. Math.round, truncate)","Verify the numeric value fits in a 64-bit signed integer; use string or float typing for larger magnitudes","Inspect the JSON payload at the failing path and fix the upstream producer to emit integral numbers"],"exampleFix":"// before (schema)\nscore int\n// JSON: {\"score\": 0.87}\n// after (schema)\nscore float  // or round before: Math.round(0.87) => 1","handlingStrategy":"validation","validationCode":"function ensureInt(n: unknown): number {\n  if (typeof n !== 'number' || !Number.isInteger(n) || n > Number.MAX_SAFE_INTEGER) {\n    throw new Error(`Expected integer, got ${n}`);\n  }\n  return n;\n}\n// run over JSON payload fields typed as int before conversion","typeGuard":null,"tryCatchPattern":"try {\n  value = parseJsonToBamlValue(json, schema);\n} catch (e) {\n  if (String(e).startsWith('Expected integer')) {\n    // round/fix the offending field or change schema to float\n  }\n  throw e;\n}","preventionTips":["Match schema types to actual JSON value kinds (float vs int)","Round/truncate LLM-emitted numbers before conversion","Validate numeric ranges (i64) of incoming payloads"],"tags":["baml","json","integer","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"}