{"record":{"id":"e56b9ddbbc0fec3f","repo":"BoundaryML/baml","slug":"error-while-parsing-llm-response-for-function-function-name","errorCode":null,"errorMessage":"error while parsing LLM response for function {function_name}","messagePattern":"error while parsing LLM response for function (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"engine/language_client_ruby/ext/ruby_ffi/src/lib.rs","lineNumber":283,"sourceCode":"        allow_partials: bool,\n        ctx: &RuntimeContextManager,\n        type_registry: Option<&types::type_builder::TypeBuilder>,\n        client_registry: Option<&types::client_registry::ClientRegistry>,\n        env_vars: HashMap<String, String>,\n    ) -> Result<magnus::Value> {\n        let parsed = rb_self\n            .inner\n            .parse_llm_response(\n                function_name.clone(),\n                llm_response,\n                allow_partials,\n                &ctx.inner,\n                type_registry.map(|t| &t.inner),\n                client_registry.map(|c| c.inner.borrow_mut()).as_deref(),\n                env_vars,\n            )\n            .map_err(|e| {\n                Error::new(\n                    ruby.exception_runtime_error(),\n                    format!(\n                        \"{:?}\",\n                        e.context(format!(\n                            \"error while parsing LLM response for function {function_name}\"\n                        ))\n                    ),\n                )\n            })?;\n\n        ruby_to_json::RubyToJson::serialize_baml(ruby, types, partial_types, allow_partials, parsed)\n            .map_err(|e| {\n                magnus::Error::new(\n                    ruby.exception_type_error(),\n                    format!(\"failed coercing BAML value to Ruby value: {e:?}\"),\n                )\n            })\n    }","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_ruby/ext/ruby_ffi/src/lib.rs#L265-L301","documentation":"Raised when parse_llm_response cannot parse/validate a raw LLM response against the function's output schema. The anyhow context carries the underlying parse or validation error, embedded in the raised RuntimeError with {:?}.","triggerScenarios":"Baml.parse_llm_response(function_name, response, ctx, type_registry, client_registry, env_vars) is given an LLM response string that doesn't match the expected BAML output type (malformed JSON output, missing fields, wrong shapes).","commonSituations":"Manually piping a provider response (e.g. captured from logs) back into BAML; the model produced invalid JSON or hallucinated fields; using the wrong type_registry for the function; truncation of long responses.","solutions":["Read the {:?} detail to see which field/shape failed validation.","Verify the raw response is complete and matches the function's declared output type.","Ensure the type_registry passed corresponds to the same compiled .baml version as the function.","If the model output is unreliable, tighten the prompt, lower temperature, or add retry/fallback client policy in the .baml config."],"exampleFix":"// before\nraw = File.read('captured_response.txt') # truncated JSON\nparsed = Baml.parse_llm_response('ExtractResume', raw, ctx, registry, nil, ENV)\n// after\nraw = File.read('captured_response.txt')\nraise 'truncated response' unless raw.end_with?('}')\nparsed = Baml.parse_llm_response('ExtractResume', raw, ctx, registry, nil, ENV)","handlingStrategy":"validation","validationCode":"raise ArgumentError, 'response must be a non-empty String' unless response.is_a?(String) && !response.strip.empty?\nJSON.parse(response) if response.strip.start_with?('{', '[') # fail fast on malformed JSON before BAML parses","typeGuard":null,"tryCatchPattern":"begin\n  parsed = Baml.parse_llm_response(fn, response, ctx, registry, nil, ENV.to_h)\nrescue RuntimeError => e\n  if e.message.include?('error while parsing LLM response')\n    logger.warn(\"LLM output for #{fn} failed schema validation: #{e.message}\")\n  end\n  raise\nend","preventionTips":["Match the type_registry version to the .baml the function was compiled from.","Confirm raw responses are complete (not truncated) before parsing.","Pre-validate that response body parses as JSON when output type is object-like.","Configure BAML retry/fallback clients to reduce malformed-output incidence."],"tags":["parsing","validation","llm","ruby"],"backgroundTag":"schema-validation-failed","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"}