Skip to content

getResponse() throws 'Invalid final response' for "tool-only" turns #45

Description

@thomai-d

Problem

My system prompt instructs the model to not respond with text if not needed.
Sometimes, the model just calls the correct tools and does not return text.
In that case, getResponse() falsely throws Error: Invalid final response: empty or invalid output.

Suggested solution

I think the check in validateFinalResponse is too strict and should not throw when the model used tools in that turn.

Affected Version

0.7.0 / main-branch

Minimal repro

import { OpenRouter, tool } from '@openrouter/agent'
import { z } from 'zod'

const API_KEY = process.env.OPENROUTER_API_KEY
const MODEL = 'openai/gpt-5.4-mini'

const client = new OpenRouter({ apiKey: API_KEY })

const noteTool = tool({
  name: 'save_note',
  description: 'Save a note. Call this when the user asks you to note something down.',
  inputSchema: z.object({ text: z.string() }),
  execute: async ({ text }) => {
    console.log(`[tool] save_note called with: "${text}"`)
    return { saved: true }
  },
})

const result = client.callModel({
  model: MODEL,
  instructions: 'When a tool call fully satisfies the user request, do not add any follow-up text. Respond with only the tool call.',
  input: [{ role: 'user', content: 'Please note: buy milk.' }],
  tools: [noteTool],
})

const response = await result.getResponse()

Result:

$ node repro.mjs 
[tool] save_note called with: "buy milk"
file:///path/to/openrouter-repro/node_modules/.pnpm/@openrouter+agent@0.7.0/node_modules/@openrouter/agent/esm/lib/model-result.js:872
            throw new Error('Invalid final response: empty or invalid output');
                  ^

Error: Invalid final response: empty or invalid output
    at ModelResult.validateFinalResponse (file:///path/to/openrouter-repro/node_modules/.pnpm/@openrouter+agent@0.7.0/node_modules/@openrouter/agent/esm/lib/model-result.js:872:19)
    at file:///path/to/openrouter-repro/node_modules/.pnpm/@openrouter+agent@0.7.0/node_modules/@openrouter/agent/esm/lib/model-result.js:1500:18
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
    at async ModelResult.getResponse (file:///path/to/openrouter-repro/node_modules/.pnpm/@openrouter+agent@0.7.0/node_modules/@openrouter/agent/esm/lib/model-result.js:1533:9)
    at async file:///path/to/openrouter-repro/repro.mjs:26:18

Node.js v22.22.2

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