Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(topics): dont send max_tokens to eu openai models (#18727)

Nimar3e41aa7beaf3
diff --git a/packages/shared/src/server/llm/ai-sdk/providers/openai.test.ts b/packages/shared/src/server/llm/ai-sdk/providers/openai.test.ts
index 6b1e9f3f25..a3d4e541bc 100644
--- a/packages/shared/src/server/llm/ai-sdk/providers/openai.test.ts
+++ b/packages/shared/src/server/llm/ai-sdk/providers/openai.test.ts
@@ -113,4 +113,22 @@ describe("isOpenAICompatibleEndpoint", () => {
it("does not enable passthrough mode for malformed URLs", () => {
expect(isOpenAICompatibleEndpoint("localhost:8080")).toBe(false);
});
+
+ it.each([
+ "https://eu.api.openai.com/v1",
+ "https://us.api.openai.com/v1",
+ "https://au.api.openai.com/v1",
+ "https://ca.api.openai.com/v1",
+ "https://EU.API.OPENAI.COM./v1",
+ ])("uses the native OpenAI provider for %s", (url) => {
+ expect(isOpenAICompatibleEndpoint(url)).toBe(false);
+ });
+
+ it.each([
+ "https://eu.api.openai.com.example.com/v1",
+ "https://notapi.openai.com/v1",
+ "https://openai.com/v1",
+ ])("keeps lookalike hosts on the compatible provider: %s", (url) => {
+ expect(isOpenAICompatibleEndpoint(url)).toBe(true);
+ });
});
diff --git a/packages/shared/src/server/llm/ai-sdk/providers/openai.ts b/packages/shared/src/server/llm/ai-sdk/providers/openai.ts
index 52ff889fb5..2c0b2defdc 100644
--- a/packages/shared/src/server/llm/ai-sdk/providers/openai.ts
+++ b/packages/shared/src/server/llm/ai-sdk/providers/openai.ts
@@ -96,8 +96,11 @@ export function isOpenAICompatibleEndpoint(
return false;
}
- const hostname = url.hostname;
- return hostname !== "api.openai.com";
+ const hostname = url.hostname.replace(/\.$/, "");
+ // Regional OpenAI API hosts use the same native request contract.
+ return (
+ hostname !== "api.openai.com" && !hostname.endsWith(".api.openai.com")
+ );
} catch {
return false;
}
diff --git a/packages/shared/src/server/topics/text.test.ts b/packages/shared/src/server/topics/text.test.ts
index 14e7fe3a25..724f814e22 100644
--- a/packages/shared/src/server/topics/text.test.ts
+++ b/packages/shared/src/server/topics/text.test.ts
@@ -272,68 +272,87 @@ describe("generateTopicText", () => {
});
});
- it("reports automatic cache usage and keeps inference successful when tracing fails", async () => {
- publishToOtelIngestionQueue.mockRejectedValueOnce(new Error("queue down"));
- vi.stubGlobal(
- "fetch",
- vi.fn(async () =>
- Response.json({
- id: "chatcmpl-1",
- object: "chat.completion",
- created: 1,
- model: "gpt-6-luna",
- choices: [
- {
- index: 0,
- finish_reason: "stop",
- message: {
- role: "assistant",
- content: JSON.stringify({ summary: "Account access" }),
+ it.each([
+ { model: "gpt-6-luna", baseURL: undefined },
+ { model: "gpt-6-luna", baseURL: "https://eu.api.openai.com/v1" },
+ { model: "gpt-5.6-terra", baseURL: "https://eu.api.openai.com/v1" },
+ { model: "gpt-5-mini", baseURL: "https://eu.api.openai.com/v1" },
+ { model: "gpt-6-luna", baseURL: "https://us.api.openai.com/v1" },
+ ])(
+ "sends the completion token limit, records cache usage, and tolerates tracing failures for $model at $baseURL",
+ async ({ model, baseURL }) => {
+ publishToOtelIngestionQueue.mockRejectedValueOnce(
+ new Error("queue down"),
+ );
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () =>
+ Response.json({
+ id: "chatcmpl-1",
+ object: "chat.completion",
+ created: 1,
+ model,
+ choices: [
+ {
+ index: 0,
+ finish_reason: "stop",
+ message: {
+ role: "assistant",
+ content: JSON.stringify({ summary: "Account access" }),
+ },
},
+ ],
+ usage: {
+ prompt_tokens: 3000,
+ completion_tokens: 200,
+ total_tokens: 3200,
+ prompt_tokens_details: { cached_tokens: 2560 },
},
- ],
- usage: {
- prompt_tokens: 3000,
- completion_tokens: 200,
- total_tokens: 3200,
- prompt_tokens_details: { cached_tokens: 2560 },
- },
- }),
- ),
- );
+ }),
+ ),
+ );
- const result = await generateTopicText({
- model: {
- slot: "summary",
- provider: "openai-topics",
- adapter: LLMAdapter.OpenAI,
- model: "gpt-6-luna",
- connection: { secretKey: encrypt("sk-test") },
- },
- system: [{ text: "System prompt and facets.", cache: true }],
- input: "I cannot sign in.",
- schema: z.object({ summary: z.string() }),
- maxOutputTokens: 256,
- trace,
- });
+ const result = await generateTopicText({
+ model: {
+ slot: "summary",
+ provider: "openai-topics",
+ adapter: LLMAdapter.OpenAI,
+ model,
+ connection: { secretKey: encrypt("sk-test"), baseURL },
+ },
+ system: [{ text: "System prompt and facets.", cache: true }],
+ input: "I cannot sign in.",
+ schema: z.object({ summary: z.string() }),
+ maxOutputTokens: 256,
+ trace,
+ });
- expect(tracedGeneration()).toMatchObject({
- "langfuse.observation.output": JSON.stringify({
- summary: "Account access",
- }),
- "langfuse.observation.model.name": "gpt-6-luna",
- "langfuse.observation.usage_details": JSON.stringify({
- input: 440,
- output: 200,
- total: 3200,
- input_cached_tokens: 2560,
- }),
- });
- expect(result.usage).toMatchObject({
- inputTokens: 3000,
- cacheReadTokens: 2560,
- });
- });
+ expect(tracedGeneration()).toMatchObject({
+ "langfuse.observation.output": JSON.stringify({
+ summary: "Account access",
+ }),
+ "langfuse.observation.model.name": model,
+ "langfuse.observation.usage_details": JSON.stringify({
+ input: 440,
+ output: 200,
+ total: 3200,
+ input_cached_tokens: 2560,
+ }),
+ });
+ expect(result.usage).toMatchObject({
+ inputTokens: 3000,
+ cacheReadTokens: 2560,
+ });
+ const fetch = vi.mocked(globalThis.fetch);
+ const request = new Request(...fetch.mock.calls[0]);
+ expect(request.url).toBe(
+ `${baseURL ?? "https://api.openai.com/v1"}/chat/completions`,
+ );
+ const body = (await request.json()) as Record<string, unknown>;
+ expect(body.max_tokens).toBeUndefined();
+ expect(body.max_completion_tokens).toBe(256);
+ },
+ );
it("retains SDK usage when no output is generated", async () => {
vi.stubGlobal(
"fetch",