🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(chatml): avoid microsoft adapter for pydantic tool responses (#14122)
Hassieb Pakzadfbde63430715
diff --git a/packages/shared/src/utils/chatml/adapters/microsoft-agent.ts b/packages/shared/src/utils/chatml/adapters/microsoft-agent.tsindex 7be0c37ec0..619ae1cbaf 100644--- a/packages/shared/src/utils/chatml/adapters/microsoft-agent.ts+++ b/packages/shared/src/utils/chatml/adapters/microsoft-agent.ts@@ -32,6 +32,36 @@ const MicrosoftAgentMessageSchema = z.looseObject({// Array of Microsoft Agent messagesconst MicrosoftAgentMessagesSchema = z.array(MicrosoftAgentMessageSchema);+function isRecord(value: unknown): value is Record<string, unknown> {+ return Boolean(value) && typeof value === "object";+}++function hasPydanticAiMessageMarkers(data: unknown): boolean {+ const messages = Array.isArray(data)+ ? data+ : isRecord(data) && Array.isArray(data.messages)+ ? data.messages+ : isRecord(data)+ ? [data]+ : [];++ return messages.some((msg) => {+ if (!isRecord(msg)) return false;+ if (!Array.isArray(msg.parts)) return false;++ return msg.parts.some((part) => {+ if (!isRecord(part)) return false;++ return (+ part.type === "thinking" ||+ part.type === "redacted_thinking" ||+ (part.type === "tool_call_response" &&+ ("result" in part || msg.role === "user"))+ );+ });+ });+}+/*** Extract tool calls and text content from parts array* Handles: text, tool_call, tool_call_response@@ -281,6 +311,13 @@ export const microsoftAgentAdapter: ProviderAdapter = {);if (providerName === "microsoft.agent_framework") return true;+ if (+ hasPydanticAiMessageMarkers(ctx.metadata) ||+ hasPydanticAiMessageMarkers(ctx.data)+ ) {+ return false;+ }+// STRUCTURAL: Schema-based detection on metadataif (MicrosoftAgentMessagesSchema.safeParse(ctx.metadata).success)return true;diff --git a/worker/src/__tests__/chatml/adapters/microsoft-agent.test.ts b/worker/src/__tests__/chatml/adapters/microsoft-agent.test.tsindex 1c3e9b8246..74d7eaef23 100644--- a/worker/src/__tests__/chatml/adapters/microsoft-agent.test.ts+++ b/worker/src/__tests__/chatml/adapters/microsoft-agent.test.ts@@ -59,6 +59,39 @@ describe("Microsoft Agent Framework Adapter", () => {expect(microsoftAgentAdapter.detect({ data: input })).toBe(true);});+ it("should not detect pydantic-ai tool response messages", () => {+ const input = [+ {+ role: "user",+ parts: [{ type: "text", content: "Look up the sample record" }],+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool_call",+ id: "tooluse_123",+ name: "lookup_sample_record",+ arguments: '{"record_id":"record-123"}',+ },+ ],+ },+ {+ role: "user",+ parts: [+ {+ type: "tool_call_response",+ id: "tooluse_123",+ name: "lookup_sample_record",+ result: { status: "not_found", message: "No record found" },+ },+ ],+ },+ ];++ expect(microsoftAgentAdapter.detect({ data: input })).toBe(false);+ });+it("should not detect OpenAI format without parts", () => {expect(microsoftAgentAdapter.detect({diff --git a/worker/src/__tests__/chatml/adapters/pydantic-ai.test.ts b/worker/src/__tests__/chatml/adapters/pydantic-ai.test.tsindex e027d473a3..cea0ce6f1d 100644--- a/worker/src/__tests__/chatml/adapters/pydantic-ai.test.ts+++ b/worker/src/__tests__/chatml/adapters/pydantic-ai.test.ts@@ -152,6 +152,67 @@ describe("Pydantic AI Adapter", () => {expect(result.data?.[3].tools).toHaveLength(2); // Available tools attached});+ it("should select pydantic-ai for tool response messages without metadata", () => {+ const input = [+ {+ role: "user",+ parts: [+ {+ type: "text",+ content: "Look up the sample record",+ },+ ],+ },+ {+ role: "assistant",+ parts: [+ {+ type: "tool_call",+ id: "tooluse_123",+ name: "lookup_sample_record",+ arguments: '{"record_id":"record-123"}',+ },+ ],+ },+ {+ role: "user",+ parts: [+ {+ type: "tool_call_response",+ id: "tooluse_123",+ name: "lookup_sample_record",+ result: {+ status: "not_found",+ message: "No record found",+ },+ },+ ],+ },+ ];++ const adapter = selectAdapter({ data: input });+ expect(adapter.id).toBe("pydantic-ai");++ const result = normalizeInput(input);++ expect(result.success).toBe(true);+ expect(result.data?.[0]).toMatchObject({+ role: "user",+ content: "Look up the sample record",+ });+ expect(result.data?.[1].tool_calls?.[0]).toMatchObject({+ id: "tooluse_123",+ name: "lookup_sample_record",+ arguments: '{"record_id":"record-123"}',+ type: "function",+ });+ expect(result.data?.[2]).toMatchObject({+ role: "tool",+ tool_call_id: "tooluse_123",+ content: '{"status":"not_found","message":"No record found"}',+ });+ });+it("should extract thinking parts", () => {const output = [{