Skip to content

Commit c388e78

Browse files
committed
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
1 parent a96ad34 commit c388e78

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

.speakeasy/in.openapi.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6189,7 +6189,9 @@ components:
61896189
type: string
61906190
object:
61916191
type: string
6192-
const: text_completion
6192+
enum:
6193+
- text_completion
6194+
- chat.completion
61936195
created:
61946196
type: number
61956197
model:

src/models/completionresponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { SDKValidationError } from "./errors/sdkvalidationerror.js";
1919

2020
export type CompletionResponse = {
2121
id: string;
22-
object: "text_completion";
22+
object: "text_completion" | "chat.completion";
2323
created: number;
2424
model: string;
2525
provider?: string | undefined;
@@ -34,7 +34,7 @@ export const CompletionResponse$inboundSchema: z.ZodType<
3434
unknown
3535
> = z.object({
3636
id: z.string(),
37-
object: z.literal("text_completion"),
37+
object: z.enum(["text_completion", "chat.completion"]),
3838
created: z.number(),
3939
model: z.string(),
4040
provider: z.string().optional(),

tests/e2e/completions.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { beforeAll, describe, expect, it } from 'vitest';
2+
import { OpenRouter } from '../../src/sdk/sdk.js';
3+
4+
describe('Completions E2E Tests', () => {
5+
let client: OpenRouter;
6+
7+
beforeAll(() => {
8+
const apiKey = process.env.OPENROUTER_API_KEY;
9+
if (!apiKey) {
10+
throw new Error('OPENROUTER_API_KEY environment variable is required for e2e tests');
11+
}
12+
13+
client = new OpenRouter({
14+
apiKey,
15+
});
16+
});
17+
18+
describe('completions.generate()', () => {
19+
it('should successfully generate a text completion', async () => {
20+
const response = await client.completions.generate({
21+
model: 'meta-llama/llama-3.2-1b-instruct',
22+
prompt: '5 + 7 = ',
23+
maxTokens: 10,
24+
stream: false,
25+
});
26+
27+
expect(response).toBeDefined();
28+
expect(response.id).toBeDefined();
29+
// API returns chat.completion, SDK should accept both
30+
expect(['text_completion', 'chat.completion']).toContain(response.object);
31+
expect(Array.isArray(response.choices)).toBe(true);
32+
expect(response.choices.length).toBeGreaterThan(0);
33+
}, 30000);
34+
});
35+
});

0 commit comments

Comments
 (0)