Skip to content

callModel throws SDKValidationError after a tool whose execute returns nothing #167

Backlog

Description

@gshost1

What happens

A tool whose execute returns nothing (async () => { ... }, common for side-effect tools like "log" or "notify") breaks callModel. The tool runs, but the follow-up request is never sent. The SDK rejects it with SDKValidationError: Input validation failed and getText() rejects.

The function_call_output is built with JSON.stringify(result). For undefined that returns undefined, not a string, so the item goes out with output: undefined. The SDK's request schema requires output to be a string or a content array. The tool's side effect has already happened when the run fails.

Minimal repro (no network, no API key; real SDK request path with a mocked HTTPClient). Save as packages/agent/tests/unit/void-tool-output.test.ts:

import { OpenRouterCore } from '@openrouter/sdk/core';
import { HTTPClient } from '@openrouter/sdk/lib/http';
import { describe, expect, it } from 'vitest';
import { z } from 'zod/v4';
import { callModel } from '../../src/inner-loop/call-model.js';
import { tool } from '../../src/lib/tool.js';

function wireResponse(output: unknown[]) {
  return {
    id: 'resp_1',
    object: 'response',
    created_at: 0,
    completed_at: 0,
    model: 'test-model',
    status: 'completed',
    output,
    error: null,
    incomplete_details: null,
    temperature: null,
    top_p: null,
    presence_penalty: null,
    frequency_penalty: null,
    metadata: null,
    instructions: null,
    tools: [],
    tool_choice: 'auto',
    parallel_tool_calls: false,
  };
}

function sse(response: unknown): Response {
  const event = {
    type: 'response.completed',
    response,
    sequence_number: 1,
  };
  return new Response(`data: ${JSON.stringify(event)}\n\ndata: [DONE]\n\n`, {
    status: 200,
    headers: {
      'content-type': 'text/event-stream',
    },
  });
}

describe('tool whose execute returns nothing', () => {
  it('sends a valid function_call_output and finishes the run', async () => {
    const bodies: Array<{
      input?: Array<{
        type?: string;
        output?: unknown;
      }>;
    }> = [];
    const httpClient = new HTTPClient();
    httpClient.request = async (request: Request) => {
      bodies.push(await request.clone().json());
      if (bodies.length === 1) {
        return sse(
          wireResponse([
            {
              type: 'function_call',
              id: 'fc_1',
              call_id: 'call_1',
              name: 'log_event',
              arguments: '{"event":"signup"}',
              status: 'completed',
            },
          ]),
        );
      }
      return sse(
        wireResponse([
          {
            type: 'message',
            id: 'msg_1',
            role: 'assistant',
            status: 'completed',
            content: [
              {
                type: 'output_text',
                text: 'Logged.',
                annotations: [],
              },
            ],
          },
        ]),
      );
    };
    const client = new OpenRouterCore({
      apiKey: 'test-key',
      httpClient,
    });

    const logged: string[] = [];
    const logEvent = tool({
      name: 'log_event',
      inputSchema: z.object({
        event: z.string(),
      }),
      execute: async ({ event }) => {
        logged.push(event);
      },
    });

    const result = callModel(client, {
      model: 'test-model',
      input: 'Log a signup event.',
      tools: [
        logEvent,
      ] as const,
    });

    await expect(result.getText()).resolves.toBe('Logged.');
    expect(logged).toEqual([
      'signup',
    ]);
    expect(bodies).toHaveLength(2);
    const output = bodies[1]?.input?.find((item) => item.type === 'function_call_output');
    expect(typeof output?.output).toBe('string');
  });
});

Run: cd packages/agent && npx vitest run tests/unit/void-tool-output.test.ts --coverage.enabled=false

Expected: the output is sent as a string (for example "null"), the model gets its follow-up turn, and getText() resolves to Logged..

Actual: only one HTTP request goes out, then:

AssertionError: promise rejected "SDKValidationError: Input validation fail… { …(2) }" instead of resolving
Caused by: SDKValidationError: Input validation failed: [
  ...
                "path": [
                  "output"
                ],
                "message": "Invalid input: expected string, received undefined"

Where:

  • computeToolOutputForModel returns JSON.stringify(value.result.result) for tools without toModelOutput and on the toModelOutput fallback path (packages/agent/src/lib/model-result.ts).
  • unsentResultsToAPIFormat (packages/agent/src/lib/conversation-state.ts) does the same for approved and resumed calls.
  • formatToolResultForModel (packages/agent/src/lib/tool-executor.ts) does too; its return type says string.
  • @openrouter/sdk's FunctionCallOutputItem$outboundSchema requires output: string | content[].

Fix: I have a PR ready: one stringifyToolOutput helper (JSON.stringify(value) ?? 'null') used at those sites.

Versions: @openrouter/agent 1.2.2 (npm latest) with @openrouter/sdk 1.5.121; also main at afb2641 (sdk 1.5.85). vitest 4.1.10, Node 24+, Linux x86_64.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions