From c388e78b90c9b532f1ca9a6488a2f435457d0211 Mon Sep 17 00:00:00 2001 From: Blake Ledden Date: Sat, 20 Dec 2025 22:07:36 -0800 Subject: [PATCH 1/2] Accept both text_completion and chat.completion in completions response The OpenRouter API returns object: "chat.completion" for the /completions endpoint, but the SDK was only accepting "text_completion". This caused validation errors when using the completions.generate() method. This change updates the CompletionResponse type and Zod schema to accept either value, since the API behavior may vary or change. Also added an E2E test to verify completions work correctly. Fixes #101 --- .speakeasy/in.openapi.yaml | 4 +++- src/models/completionresponse.ts | 4 ++-- tests/e2e/completions.test.ts | 35 ++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 tests/e2e/completions.test.ts diff --git a/.speakeasy/in.openapi.yaml b/.speakeasy/in.openapi.yaml index 4047ff25..c4a60113 100644 --- a/.speakeasy/in.openapi.yaml +++ b/.speakeasy/in.openapi.yaml @@ -6189,7 +6189,9 @@ components: type: string object: type: string - const: text_completion + enum: + - text_completion + - chat.completion created: type: number model: diff --git a/src/models/completionresponse.ts b/src/models/completionresponse.ts index 242ad6bd..b628e10b 100644 --- a/src/models/completionresponse.ts +++ b/src/models/completionresponse.ts @@ -19,7 +19,7 @@ import { SDKValidationError } from "./errors/sdkvalidationerror.js"; export type CompletionResponse = { id: string; - object: "text_completion"; + object: "text_completion" | "chat.completion"; created: number; model: string; provider?: string | undefined; @@ -34,7 +34,7 @@ export const CompletionResponse$inboundSchema: z.ZodType< unknown > = z.object({ id: z.string(), - object: z.literal("text_completion"), + object: z.enum(["text_completion", "chat.completion"]), created: z.number(), model: z.string(), provider: z.string().optional(), diff --git a/tests/e2e/completions.test.ts b/tests/e2e/completions.test.ts new file mode 100644 index 00000000..48dc64ac --- /dev/null +++ b/tests/e2e/completions.test.ts @@ -0,0 +1,35 @@ +import { beforeAll, describe, expect, it } from 'vitest'; +import { OpenRouter } from '../../src/sdk/sdk.js'; + +describe('Completions E2E Tests', () => { + let client: OpenRouter; + + beforeAll(() => { + const apiKey = process.env.OPENROUTER_API_KEY; + if (!apiKey) { + throw new Error('OPENROUTER_API_KEY environment variable is required for e2e tests'); + } + + client = new OpenRouter({ + apiKey, + }); + }); + + describe('completions.generate()', () => { + it('should successfully generate a text completion', async () => { + const response = await client.completions.generate({ + model: 'meta-llama/llama-3.2-1b-instruct', + prompt: '5 + 7 = ', + maxTokens: 10, + stream: false, + }); + + expect(response).toBeDefined(); + expect(response.id).toBeDefined(); + // API returns chat.completion, SDK should accept both + expect(['text_completion', 'chat.completion']).toContain(response.object); + expect(Array.isArray(response.choices)).toBe(true); + expect(response.choices.length).toBeGreaterThan(0); + }, 30000); + }); +}); From 90a451f6803d41d4aff02ef8ab80c75c62400202 Mon Sep 17 00:00:00 2001 From: Blake Ledden Date: Sun, 21 Dec 2025 00:25:06 -0800 Subject: [PATCH 2/2] chore: remove generated file changes per CONTRIBUTING.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The TypeScript SDK repo does not accept direct changes to generated code. Only the OpenAPI spec change and E2E test should be included. The maintainers will regenerate the SDK after merging. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/models/completionresponse.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/completionresponse.ts b/src/models/completionresponse.ts index b628e10b..242ad6bd 100644 --- a/src/models/completionresponse.ts +++ b/src/models/completionresponse.ts @@ -19,7 +19,7 @@ import { SDKValidationError } from "./errors/sdkvalidationerror.js"; export type CompletionResponse = { id: string; - object: "text_completion" | "chat.completion"; + object: "text_completion"; created: number; model: string; provider?: string | undefined; @@ -34,7 +34,7 @@ export const CompletionResponse$inboundSchema: z.ZodType< unknown > = z.object({ id: z.string(), - object: z.enum(["text_completion", "chat.completion"]), + object: z.literal("text_completion"), created: z.number(), model: z.string(), provider: z.string().optional(),