BoundaryML/baml · error

InternalError

InternalError

Error message

Failed to serialize functions: {:?}

What it means

The language server's internal API endpoint that lists all functions fails when serde_json cannot serialize the collected all_functions value into JSON. Serialization is essentially infallible for normal data, so this usually indicates a non-serializable type (e.g. a map with non-string keys) sneaking into the value.

Source

Thrown at engine/language_server/src/server/api.rs:117

                            .iter()
                            .map(|f| BamlFunctionResult {
                                name: f.name.clone(),
                                span: BamlFunctionSpan {
                                    file_path: f.span.file_path.clone(),
                                    start: f.span.start,
                                    end: f.span.end,
                                },
                            })
                            .collect::<Vec<BamlFunctionResult>>();

                        all_functions.extend(functions);
                    }

                    let result = serde_json::to_value(all_functions);
                    if let Ok(result) = result {
                        Ok((result,))
                    } else {
                        Err(anyhow::anyhow!(
                            "Failed to serialize functions: {:?}",
                            result
                        ))
                    }
                };
                if let Ok((result,)) = result {
                    responder.respond(id, Ok(result)).unwrap();
                } else {
                    // no action
                    // responder.respond(id, Err(result.unwrap_err())).unwrap();
                }
            });
        }
        "requestDiagnostics" => {
            // tracing::info!("---- requestDiagnostics");
            return Task::local(move |session, notifier, _requester, responder| {
                let result: anyhow::Result<()> = (|| {
                    // tracing::info!("requestDiagnostics: {:?}", req.params);

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Inspect all_functions for values not representable in JSON (non-string keys, unsupported numeric types) and convert them to strings.
  2. Include the serde error in the message (currently the Err value is logged, not the cause) to diagnose.
  3. Derive Serialize consistently on all function metadata types.
  4. Map the failure to a proper JSON-RPC internal error response instead of a bare anyhow error.

Example fix

// before
Err(anyhow::anyhow!("Failed to serialize functions: {:?}", result))
// after
Err(anyhow::anyhow!("Failed to serialize functions: {:?}", result.err()))
Defensive patterns

Strategy: fallback

Validate before calling

const kind = getSymbolKindAt(uri, position);
if (!['class','enum','typeAlias'].includes(kind)) return;

Type guard

const isRenamableSymbol = (k: string | undefined): k is 'class'|'enum'|'typeAlias' =>
  k === 'class' || k === 'enum' || k === 'typeAlias';

Try / catch

try {
  await renameSymbol(uri, position, newName);
} catch (e) {
  if (String(e.message).startsWith('Cannot rename symbol')) {
    notifyUser('Only classes, enums, and type aliases can be renamed');
  }
}

Prevention

When it happens

Trigger: The api.rs request handler builds all_functions and calls serde_json::to_value, which returns Err — typically because a function entry contains a non-JSON-serializable structure (non-string map keys, NaN in floats, etc.).

Common situations: Custom or newly added function metadata with exotic types breaking serde's JSON model; concurrent schema changes between collection and serialization; rarely, extremely large payloads causing serializer limits.

Understand the failure class

Background: "JSON serialization failed", "not JSON serializable", "Failed to serialize": why JSON marshaling errors happen and how to fix them — this error's family across 46 libraries.

Related errors


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