Malformed Tool Call Handler (providers/malformed-tool-call.ts)

vellum-assistant · assistant/src/providers/malformed-tool-call.ts · typescript · MIT
ai, lang:typescript, tag:knn-classified, tag:error-handling, tag:retry

Detects and handles malformed AI tool calls and finish reasons from language models. Reuse when implementing robust LLM retry logic for invalid function calls.

// Source: https://github.com/vellum-ai/vellum-assistant | License: MIT | Copyright 2025 Vellum AI
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Upstream finish reasons that mean the model tried to call a tool but the
 * call did not parse.
 *
 * Gemini reports these as `MALFORMED_FUNCTION_CALL` (the call was not valid)
 * and `UNEXPECTED_TOOL_CALL` (a call the request did not allow). The native
 * Gemini API returns them as `candidate.finishReason`; OpenRouter normalizes
 * them away in `finish_reason` and passes the raw value through in
 * `native_finish_reason`. Such responses often carry the tail of the broken
 * call as plain assistant text, so they must never be taken as a final answer.
 * Providers throw a status-less `ProviderError` carrying
 * {@link MALF

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key