{"record":{"id":"c4ab5bfac6e5508f","repo":"BoundaryML/baml","slug":"json-args-must-be-a-json-object-got-json","errorCode":null,"errorMessage":"--json-args must be a JSON object, got: {json}","messagePattern":"--json-args must be a JSON object, got: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_exec/src/dispatch.rs","lineNumber":210,"sourceCode":"}\n\nasync fn build_args_from_signature_with_context(\n    engine: &Arc<BexEngine>,\n    cli_values: HashMap<String, BexExternalValue>,\n    json_args: Option<&serde_json::Value>,\n    param_names: &[String],\n    param_types: &[RuntimeTy],\n    param_has_default: &[bool],\n    helper_context: &HelperCallContext,\n) -> Result<Vec<BexCallArg>> {\n    let _ = param_types;\n    let mut merged: HashMap<String, RawArg> = HashMap::new();\n\n    // `--json-args` first (lower priority — CLI overrides).\n    if let Some(json) = json_args {\n        let obj = json\n            .as_object()\n            .ok_or_else(|| anyhow!(\"--json-args must be a JSON object, got: {json}\"))?;\n        for (key, value) in obj {\n            // Re-stringify each field so the engine deserializer takes a\n            // fresh JSON document. Round-tripping via `to_string` is\n            // cheaper than implementing a parallel json-value-to-engine\n            // path; per-arg JSON is typically tiny.\n            merged.insert(key.clone(), RawArg::JsonText(value.to_string()));\n        }\n    }\n\n    // Auto-CLI flags override. By this point the clap parser has already\n    // validated structure (required flags, unknown flags) and converted\n    // each raw string to a typed primitive — structured types arrive via\n    // `--json-args` only.\n    for (key, value) in cli_values {\n        merged.insert(key, RawArg::Primitive(Box::new(value)));\n    }\n\n    let mut ordered = Vec::with_capacity(param_names.len());","sourceCodeStart":192,"sourceCodeEnd":228,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_exec/src/dispatch.rs#L192-L228","documentation":"`--json-args` must parse to a JSON object (a map of parameter name to value). If the provided JSON is any other shape (array, string, number, boolean, null), `build_args_from_signature_with_context` rejects it, echoing the offending JSON. Arrays/strings are never auto-mapped positionally.","triggerScenarios":"Passing `--json-args '[1,2]'`, `--json-args '\"hi\"'`, or a file/stdin whose content is valid JSON but not an object; the check is `json.as_object()` in dispatch.rs:210.","commonSituations":"Feeding a top-level JSON array of arguments, forgetting `{}` braces, or pointing `@file` at a file that contains a JSON array instead of a parameter map.","solutions":["Wrap the payload in a JSON object keyed by parameter names: `{\"param\": value}`.","If the file contains an array, restructure it to an object or pass the array as the value of an object-typed parameter.","Validate the file with jq (e.g. `jq type` should print `object`) before passing via `--json-args @file`."],"exampleFix":"// before\nbaml run myFunc -- --json-args '[\"a\", 1]'\n\n// after\nbaml run myFunc -- --json-args '{\"name\": \"a\", \"count\": 1}'","handlingStrategy":"validation","validationCode":"let json: serde_json::Value = serde_json::from_str(&raw)?;\nif !json.is_object() {\n    anyhow::bail!(\"--json-args must be an object of {param: value}\");\n}","typeGuard":"fn is_json_object(v: &serde_json::Value) -> bool { v.is_object() }","tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"--json-args must be a JSON object\") => {\n        eprintln!(\"wrap the payload as an object keyed by parameter names\");\n    }\n    other => other,\n}","preventionTips":["Always key --json-args payloads by parameter name","Validate files with `jq type` (expect `object`) before @file usage","Never pass bare arrays or scalars as --json-args"],"tags":["cli","json","arguments","baml"],"backgroundTag":"invalid-json-response","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"}