Skip to content

Remove /chat/completions from OpenAI compatibility endpoint for OpenAI SDK #22906

@nbbaier

Description

@nbbaier

Existing documentation URL(s)

What changes are you suggesting?

The OpenAI SDK example for the new AI Gateway OpenAI compatible endpoint have a base url of https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat/chat/completions. This results in a 500 error, because the OpenAI SDK automatically appends /chat/completions to baseURL.

The example should be changed to the following:

import OpenAI from "openai";
const client = new OpenAI({
  apiKey: "YOUR_PROVIDER_API_KEY", // Provider API key
  baseURL:
    "https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat", // <-- CHANGE HERE
});

const response = await client.chat.completions.create({
  model: "google-ai-studio/gemini-2.0-flash",
  messages: [{ role: "user", content: "What is Cloudflare?" }],
});

console.log(response.choices[0].message.content);

A note should also be added to make sure people understand this difference.

Additional information

No response

Metadata

Metadata

Labels

content:editRequest for content editsdocumentationDocumentation editsproduct:ai-gatewayAI Gateway: https://developers.cloudflare.com/ai-gateway/

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions