Skip to content

Conversation

@bledden
Copy link

@bledden bledden commented Dec 21, 2025

Summary

The completions.generate() method was failing with a validation error because the SDK expected object: "text_completion" but the OpenRouter API returns object: "chat.completion".

This caused the error:

ResponseValidationError: Response validation failed
ZodError: Invalid input: expected "text_completion"

Changes

  • Updated CompletionResponse type to accept either "text_completion" or "chat.completion"
  • Updated the Zod schema to use z.enum() instead of z.literal()
  • Updated the OpenAPI spec to match
  • Added E2E test for completions

Testing

const response = await client.completions.generate({
  model: 'meta-llama/llama-3.1-405b-instruct',
  prompt: '5 + 7 = ',
  maxTokens: 10,
  stream: false,
});
// Now works without validation errors

Fixes #101

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 OpenRouterTeam#101
@bledden
Copy link
Author

bledden commented Dec 21, 2025

Note: This fix allows the SDK to accept both text_completion and chat.completion as valid values for the object field in completions responses.

However, the underlying issue may be on the API side - the /api/v1/completions endpoint is returning object: "chat.completion" when it arguably should return "text_completion" to match OpenAI's completions API behavior.

The OpenRouter team may want to investigate whether the API response should be updated to return the correct object type for completions, or whether returning chat.completion is intentional behavior.

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 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Basic text completion returns incorrect object (thus fails validation)

1 participant