BoundaryML/baml · error · RuntimeError

error while parsing LLM response for function {function_name

Error message

error while parsing LLM response for function {function_name}

What it means

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 {:?}.

Source

Thrown at engine/language_client_ruby/ext/ruby_ffi/src/lib.rs:283

        allow_partials: bool,
        ctx: &RuntimeContextManager,
        type_registry: Option<&types::type_builder::TypeBuilder>,
        client_registry: Option<&types::client_registry::ClientRegistry>,
        env_vars: HashMap<String, String>,
    ) -> Result<magnus::Value> {
        let parsed = rb_self
            .inner
            .parse_llm_response(
                function_name.clone(),
                llm_response,
                allow_partials,
                &ctx.inner,
                type_registry.map(|t| &t.inner),
                client_registry.map(|c| c.inner.borrow_mut()).as_deref(),
                env_vars,
            )
            .map_err(|e| {
                Error::new(
                    ruby.exception_runtime_error(),
                    format!(
                        "{:?}",
                        e.context(format!(
                            "error while parsing LLM response for function {function_name}"
                        ))
                    ),
                )
            })?;

        ruby_to_json::RubyToJson::serialize_baml(ruby, types, partial_types, allow_partials, parsed)
            .map_err(|e| {
                magnus::Error::new(
                    ruby.exception_type_error(),
                    format!("failed coercing BAML value to Ruby value: {e:?}"),
                )
            })
    }

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Read the {:?} detail to see which field/shape failed validation.
  2. Verify the raw response is complete and matches the function's declared output type.
  3. Ensure the type_registry passed corresponds to the same compiled .baml version as the function.
  4. If the model output is unreliable, tighten the prompt, lower temperature, or add retry/fallback client policy in the .baml config.

Example fix

// before
raw = File.read('captured_response.txt') # truncated JSON
parsed = Baml.parse_llm_response('ExtractResume', raw, ctx, registry, nil, ENV)
// after
raw = File.read('captured_response.txt')
raise 'truncated response' unless raw.end_with?('}')
parsed = Baml.parse_llm_response('ExtractResume', raw, ctx, registry, nil, ENV)
Defensive patterns

Strategy: validation

Validate before calling

raise ArgumentError, 'response must be a non-empty String' unless response.is_a?(String) && !response.strip.empty?
JSON.parse(response) if response.strip.start_with?('{', '[') # fail fast on malformed JSON before BAML parses

Try / catch

begin
  parsed = Baml.parse_llm_response(fn, response, ctx, registry, nil, ENV.to_h)
rescue RuntimeError => e
  if e.message.include?('error while parsing LLM response')
    logger.warn("LLM output for #{fn} failed schema validation: #{e.message}")
  end
  raise
end

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/e56b9ddbbc0fec3f. Report an issue: GitHub.