Skip to content

Basic text completion returns incorrect object (thus fails validation) #101

@mlomb

Description

@mlomb

Hey! Basically what it says on the title.

If I run the following:

const completion = await openRouter.completions.generate({
  model: "meta-llama/llama-3.1-405b-instruct",
  prompt: "5 + 7 = ",
  stop: ["\n"],
  maxTokens: 10,
  stream: false,
});

I get the following:

ResponseValidationError: Response validation failed
    at safeParseResponse ([REDACTED]/node_modules/@openrouter/sdk/esm/lib/matchers.js:195:20)
    at matchFunc ([REDACTED]/node_modules/@openrouter/sdk/esm/lib/matchers.js:172:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async $do ([REDACTED]/node_modules/@openrouter/sdk/esm/funcs/completionsGenerate.js:76:22) {
  statusCode: 200,
  body: '\n' +
    '         \n' +
    '\n' +
    '         \n' +
    '\n' +
    '         \n' +
    '{"id":"gen-1764704027-YmgZuzDAKrAywxh1aHQW","provider":"Google","model":"meta-llama/llama-3.1-405b-instruct","object":"chat.completion","created":1764704027,"choices":[{"logprobs":null,"finish_reason":"stop","native_finish_reason":"stop","index":0,"text":"","reasoning":null}],"system_fingerprint":"","usage":{"completion_tokens":9,"extra_properties":{"google":{"traffic_type":"ON_DEMAND"}},"prompt_tokens":41,"total_tokens":50}}',
  headers: Headers {},
  contentType: 'application/json',
  rawResponse: Response {
    [Symbol(state)]: {
      aborted: false,
      rangeRequested: false,
      timingAllowPassed: true,
      requestIncludesCredentials: true,
      type: 'default',
      status: 200,
      timingInfo: {
        startTime: 536.9904,
        redirectStartTime: 0,
        redirectEndTime: 0,
        postRedirectStartTime: 536.9904,
        finalServiceWorkerStartTime: 0,
        finalNetworkResponseStartTime: 2338.4671,
        finalNetworkRequestStartTime: 603.5989,
        endTime: 0,
        encodedBodySize: 337,
        decodedBodySize: 462,
        finalConnectionTimingInfo: {
          domainLookupStartTime: 536.9904,
          domainLookupEndTime: 536.9904,
          connectionStartTime: 536.9904,
          connectionEndTime: 536.9904,
          secureConnectionStartTime: 536.9904,
          ALPNNegotiatedProtocol: undefined
        }
      },
      cacheState: '',
      statusText: 'OK',
      headersList: [REDACTED],
      urlList: [ https://openrouter.ai/api/v1/completions ],
      body: {
        stream: ReadableStream {
          [Symbol(kType)]: 'ReadableStream',
          [Symbol(kState)]: [Object: null prototype] {
            disturbed: true,
            reader: [ReadableStreamDefaultReader],
            state: 'closed',
            storedError: undefined,
            transfer: [Object: null prototype],
            controller: [ReadableByteStreamController]
          },
          [Symbol(nodejs.webstream.isClosedPromise)]: {
            promise: [Promise],
            resolve: [Function (anonymous)],
            reject: [Function (anonymous)]
          },
          [Symbol(nodejs.webstream.controllerErrorFunction)]: [Function (anonymous)]
        },
        source: null,
        length: null
      }
    },
    [Symbol(headers)]: Headers {}
  },
  cause: ZodError: [
    {
      "code": "invalid_value",
      "values": [
        "text_completion"
      ],
      "path": [
        "object"
      ],
      "message": "Invalid input: expected \"text_completion\""
    }
  ]
      at safeParseResponse.request.request ([REDACTED]/node_modules/@openrouter/sdk/esm/lib/matchers.js:172:63)
      at safeParseResponse ([REDACTED]/node_modules/@openrouter/sdk/esm/lib/matchers.js:192:19)
      at matchFunc ([REDACTED]/node_modules/@openrouter/sdk/esm/lib/matchers.js:172:17)
      at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
      at async $do ([REDACTED]/node_modules/@openrouter/sdk/esm/funcs/completionsGenerate.js:76:22),
  rawValue: {
    id: 'gen-1764704027-YmgZuzDAKrAywxh1aHQW',
    provider: 'Google',
    model: 'meta-llama/llama-3.1-405b-instruct',
    object: 'chat.completion',
    created: 1764704027,
    choices: [
      {
        logprobs: null,
        finish_reason: 'stop',
        native_finish_reason: 'stop',
        index: 0,
        text: '',
        reasoning: null
      }
    ],
    system_fingerprint: '',
    usage: {
      completion_tokens: 9,
      extra_properties: { google: { traffic_type: 'ON_DEMAND' } },
      prompt_tokens: 41,
      total_tokens: 50
    }
  },
  rawMessage: 'Response validation failed'
}

Object is chat.completion but the SDK is expecting text_completion 🤔

Btw I also tried with responseFormat: { type: "text" }, same result

Also, why it says "prompt_tokens":41? I would expect 5 + 7 = to be less tokens.

Thanks!

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