Skip to content

fix(agent): send a string function_call_output when a tool returns nothing - #168

Open
gshost1 wants to merge 2 commits into
OpenRouterTeam:mainfrom
gshost1:fix/void-tool-output
Open

gshost1 wants to merge 2 commits into
OpenRouterTeam:mainfrom
gshost1:fix/void-tool-output

Conversation

@gshost1

@gshost1 gshost1 commented Nov 24, 2026 •

Copy link
Copy Markdown

What

Adds stringifyToolOutput(value) (JSON.stringify(value) ?? 'null') in conversation-state.ts and uses it where a tool result becomes a function_call_output: computeToolOutputForModel (both paths) in model-result.ts, unsentResultsToAPIFormat, formatToolResultForModel, and toFunctionCallOutputValue (the HITL onResponseReceived path, where a hook returning undefined for a z.void() output schema hit the same error). Adds unit tests and a patch changeset for @openrouter/agent.

Why

A tool whose execute returns nothing gave JSON.stringify(undefined), which is undefined, so the function_call_output item had output: undefined. The SDK's request schema requires a string or content array, so the follow-up request failed with SDKValidationError after the tool had already run, and callModel never got its next turn. The TypeScript types accept a void execute. Fixes #167.

How tested

  • cd packages/agent && npx vitest run tests/unit/void-tool-output.test.ts --coverage.enabled=false: fails on main (3/3), passes with this change.
  • cd packages/agent && npx vitest run tests/unit/hitl-void-output.test.ts --coverage.enabled=false: fails on main (3/3), passes with this change. The test uses the real SDK request path (OpenRouterCore with a mocked HTTPClient), so it exercises the schema that rejects the request; mocking betaResponsesSend instead would not catch this.
  • pnpm run lint, pnpm run typecheck: clean.
  • pnpm run test: @openrouter/agent 1295/1295 (114 files), @openrouter/mcp 12/12, coverage thresholds pass.
  • Node 26 / pnpm 10.22.0, Linux x86_64, on main at afb2641.

Risks

  • A void result is now sent as the string "null" (same as JSON.stringify(null)). Maintainers may prefer "" or a short message; it's one line in the helper.
  • Other values JSON.stringify maps to undefined (a function, a symbol) also become "null" instead of failing validation.
  • Results that already serialized to a string are unchanged; content-array outputs are untouched.

…thing

A tool whose execute returns undefined (a side-effect tool such as
"log" or "notify") produced function_call_output with output: undefined,
because JSON.stringify(undefined) is undefined, not a string. The SDK's
request schema requires a string or content array, so the follow-up
request failed with SDKValidationError after the tool had already run and
callModel never got its next turn.

Serialize tool results through one stringifyToolOutput helper that falls
back to "null", and use it at the four JSON.stringify(result) sites.
toFunctionCallOutputValue used JSON.stringify directly, so a HITL tool whose onResponseReceived hook returns undefined (valid with outputSchema z.void()) still sent output: undefined and the next request failed SDK validation. Use the same stringifyToolOutput helper as the other function_call_output sites.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

callModel throws SDKValidationError after a tool whose execute returns nothing

1 participant