Skip to content

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

fix(chatml): guard against null scope in provider adapter detection (#14308)

Nick Lenten37e0bb928942
diff --git a/packages/shared/src/utils/chatml/adapters/aisdk.ts b/packages/shared/src/utils/chatml/adapters/aisdk.ts
index 426d8a7529..2a1089cb34 100644
--- a/packages/shared/src/utils/chatml/adapters/aisdk.ts
+++ b/packages/shared/src/utils/chatml/adapters/aisdk.ts
@@ -418,7 +418,11 @@ export const aisdkAdapter: ProviderAdapter = {
// STRONG INDICATORS: AI SDK v5 telemetry markers
if (meta && typeof meta === "object") {
- if ("scope" in meta && typeof meta.scope === "object") {
+ if (
+ "scope" in meta &&
+ meta.scope !== null &&
+ typeof meta.scope === "object"
+ ) {
const scope = meta.scope as Record<string, unknown>;
if (scope.name === "ai") return true;
}
diff --git a/packages/shared/src/utils/chatml/adapters/langgraph.ts b/packages/shared/src/utils/chatml/adapters/langgraph.ts
index 80fcdef7c4..d5b643122c 100644
--- a/packages/shared/src/utils/chatml/adapters/langgraph.ts
+++ b/packages/shared/src/utils/chatml/adapters/langgraph.ts
@@ -279,7 +279,11 @@ export const langgraphAdapter: ProviderAdapter = {
// REJECTIONS: Reject AI SDK v5, OpenAI Agents SDK, Semantic Kernel and Pydantic formats
if (meta && typeof meta === "object") {
// Check scope.name for AI SDK, OpenAI Agents, or Semantic Kernel
- if ("scope" in meta && typeof meta.scope === "object") {
+ if (
+ "scope" in meta &&
+ meta.scope !== null &&
+ typeof meta.scope === "object"
+ ) {
const scope = meta.scope as Record<string, unknown>;
// Reject AI SDK v5 (scope.name === "ai")
diff --git a/packages/shared/src/utils/chatml/adapters/openai.ts b/packages/shared/src/utils/chatml/adapters/openai.ts
index 22c8901f91..dfb3317510 100644
--- a/packages/shared/src/utils/chatml/adapters/openai.ts
+++ b/packages/shared/src/utils/chatml/adapters/openai.ts
@@ -384,7 +384,11 @@ export const openAIAdapter: ProviderAdapter = {
}
// Semantic Kernel (scope.name starts with Microsoft.SemanticKernel)
- if ("scope" in meta && typeof meta.scope === "object") {
+ if (
+ "scope" in meta &&
+ meta.scope !== null &&
+ typeof meta.scope === "object"
+ ) {
const scope = meta.scope as Record<string, unknown>;
if (
typeof scope.name === "string" &&
@@ -410,7 +414,11 @@ export const openAIAdapter: ProviderAdapter = {
if (providerName === "microsoft.agent_framework") return false;
// Pydantic ai
- if ("scope" in meta && typeof meta.scope === "object") {
+ if (
+ "scope" in meta &&
+ meta.scope !== null &&
+ typeof meta.scope === "object"
+ ) {
const scope = meta.scope as Record<string, unknown>;
if (scope.name === "pydantic-ai") {
return false;
diff --git a/worker/src/__tests__/chatml/adapters/langgraph.test.ts b/worker/src/__tests__/chatml/adapters/langgraph.test.ts
index 8bf4267ff8..3aa5140b8c 100644
--- a/worker/src/__tests__/chatml/adapters/langgraph.test.ts
+++ b/worker/src/__tests__/chatml/adapters/langgraph.test.ts
@@ -31,6 +31,12 @@ describe("LangGraph Adapter", () => {
metadata: { scope: { name: "langfuse-sdk" } },
}),
).toBe(false);
+
+ // Regression: a null scope must not throw (typeof null === "object")
+ expect(() =>
+ langgraphAdapter.detect({ metadata: { scope: null } }),
+ ).not.toThrow();
+ expect(langgraphAdapter.detect({ metadata: { scope: null } })).toBe(false);
});
it("should normalize LangGraph messages with type-to-role conversion and tool_calls", () => {
diff --git a/worker/src/__tests__/chatml/integration.test.ts b/worker/src/__tests__/chatml/integration.test.ts
index 495ccf067a..3cace624b4 100644
--- a/worker/src/__tests__/chatml/integration.test.ts
+++ b/worker/src/__tests__/chatml/integration.test.ts
@@ -112,6 +112,25 @@ describe("ChatML Integration", () => {
expect(() => normalizeInput(input)).not.toThrow();
});
+ it("should not throw when the input object has a null scope (typeof null === 'object')", () => {
+ // Regression: when no metadata is passed, normalizeInput falls back to
+ // using the input as metadata. An input object with a literal `scope: null`
+ // (e.g. Dify workflow inputs) used to crash every adapter's detect(), which
+ // does `typeof meta.scope === "object"` and then reads `meta.scope.name` —
+ // `typeof null === "object"` slipped a null through the guard.
+ const input = {
+ scope: null,
+ page_url: null,
+ page_title: null,
+ country: "ch",
+ locale: "de",
+ "sys.query": "Which biomarkers are in the Advanced Blood Test?",
+ };
+
+ expect(() => normalizeInput(input)).not.toThrow();
+ expect(() => normalizeOutput({ scope: null, answer: "..." })).not.toThrow();
+ });
+
it("should handle very large inputs", () => {
const largeContent = "x".repeat(1000000);
const input = [{ role: "user", content: largeContent }];