Skip to content

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

fix(trace-ui): tool call ordering (#10188)

Nimar033bec48b191
diff --git a/web/src/components/trace/IOPreview.tsx b/web/src/components/trace/IOPreview.tsx
index 74cac4e9ee..00d5858ad6 100644
--- a/web/src/components/trace/IOPreview.tsx
+++ b/web/src/components/trace/IOPreview.tsx
@@ -97,45 +97,47 @@ export const IOPreview: React.FC<{
outputClean,
);
- // extract all unique tools from messages and assign them sequential numbers
+ // extract all unique tools from messages (no numbering yet)
const toolsMap = new Map<
string,
{ name: string; description?: string; parameters?: Record<string, any> }
>();
- const toolNameToDefinitionNumber = new Map<string, number>();
- let toolDefinitionCounter = 0;
for (const message of messages) {
if (message.tools && Array.isArray(message.tools)) {
for (const tool of message.tools) {
if (!toolsMap.has(tool.name)) {
toolsMap.set(tool.name, tool);
- toolDefinitionCounter++;
- toolNameToDefinitionNumber.set(tool.name, toolDefinitionCounter);
}
}
}
}
- const uniqueTools = Array.from(toolsMap.values());
- // sequential numbering for tool call invocations
+ // count tool call invocations and tool calls
+ // Only number tool calls from OUTPUT messages (current invocation), not input (history)
+ const inputMessageCount = inResult.success ? inResult.data.length : 0;
let toolCallCounter = 0;
const messageToToolCallNumbers = new Map<number, number[]>();
const toolCallCounts = new Map<string, number>();
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
+ const isOutputMessage = i >= inputMessageCount; // Only output messages get numbered
+
if (message.tool_calls && Array.isArray(message.tool_calls)) {
const messageToolNumbers: number[] = [];
for (const toolCall of message.tool_calls) {
if (toolCall.name && typeof toolCall.name === "string") {
- toolCallCounter++;
- messageToolNumbers.push(toolCallCounter);
toolCallCounts.set(
toolCall.name,
(toolCallCounts.get(toolCall.name) || 0) + 1,
);
+
+ if (isOutputMessage) {
+ toolCallCounter++;
+ messageToolNumbers.push(toolCallCounter);
+ }
}
}
@@ -145,12 +147,28 @@ export const IOPreview: React.FC<{
}
}
+ // sort tools by display order (called first, then by call count)
+ const sortedTools = Array.from(toolsMap.values()).sort((a, b) => {
+ const callCountA = toolCallCounts.get(a.name) || 0;
+ const callCountB = toolCallCounts.get(b.name) || 0;
+ // Sort by called status (called first), then by call count descending
+ if (callCountA > 0 && callCountB === 0) return -1;
+ if (callCountA === 0 && callCountB > 0) return 1;
+ return callCountB - callCountA;
+ });
+
+ // assign definition numbers based on sorted display order
+ const toolNameToDefinitionNumber = new Map<string, number>();
+ sortedTools.forEach((tool, index) => {
+ toolNameToDefinitionNumber.set(tool.name, index + 1);
+ });
+
return {
canDisplayAsChat:
(inResult.success || outResult.success) && messages.length > 0,
allMessages: messages,
additionalInput: extractAdditionalInput(input),
- allTools: uniqueTools,
+ allTools: sortedTools,
toolCallCounts,
messageToToolCallNumbers,
toolNameToDefinitionNumber,
@@ -181,14 +199,7 @@ export const IOPreview: React.FC<{
<div className="mb-4 border-b border-border pb-4">
<div className="px-1 py-1 text-sm font-medium capitalize">Tools</div>
<ToolCallDefinitionCard
- tools={allTools.sort((a, b) => {
- const callCountA = toolCallCounts.get(a.name) || 0;
- const callCountB = toolCallCounts.get(b.name) || 0;
- // Sort by called status (called first), then by call count descending
- if (callCountA > 0 && callCountB === 0) return -1;
- if (callCountA === 0 && callCountB > 0) return 1;
- return callCountB - callCountA;
- })}
+ tools={allTools}
toolCallCounts={toolCallCounts}
toolNameToDefinitionNumber={toolNameToDefinitionNumber}
/>
diff --git a/web/src/utils/chatml/adapters/langgraph.ts b/web/src/utils/chatml/adapters/langgraph.ts
index 1fa49e46a3..dba952bb4f 100644
--- a/web/src/utils/chatml/adapters/langgraph.ts
+++ b/web/src/utils/chatml/adapters/langgraph.ts
@@ -291,7 +291,11 @@ export const langgraphAdapter: ProviderAdapter = {
// Check wrapped messages format
if (LangGraphWrappedSchema.safeParse(ctx.metadata).success) {
- const wrapped = ctx.metadata as { messages: unknown[] };
+ const wrapped = ctx.metadata as { messages: unknown[]; tools?: unknown };
+ // reject OpenAI Chat Completions format {tools: [...], messages: [...]}
+ if (Array.isArray(wrapped.tools)) {
+ return false;
+ }
if (LangChainMessageSchema.safeParse(wrapped.messages).success)
return true;
if (LangGraphMessageSchema.safeParse(wrapped.messages).success)
@@ -304,7 +308,11 @@ export const langgraphAdapter: ProviderAdapter = {
// Check wrapped messages format on data
if (LangGraphWrappedSchema.safeParse(ctx.data).success) {
- const wrapped = ctx.data as { messages: unknown[] };
+ const wrapped = ctx.data as { messages: unknown[]; tools?: unknown };
+ // reject OpenAI Chat Completions format {tools: [...], messages: [...]}
+ if (Array.isArray(wrapped.tools)) {
+ return false;
+ }
if (LangChainMessageSchema.safeParse(wrapped.messages).success)
return true;
if (LangGraphMessageSchema.safeParse(wrapped.messages).success)
diff --git a/web/src/utils/chatml/adapters/openai.clienttest.ts b/web/src/utils/chatml/adapters/openai.clienttest.ts
index 4728b3c9e4..ee6a8ab46f 100644
--- a/web/src/utils/chatml/adapters/openai.clienttest.ts
+++ b/web/src/utils/chatml/adapters/openai.clienttest.ts
@@ -223,6 +223,57 @@ describe("OpenAI Adapter", () => {
);
});
+ it("should handle request format with multiple tools and multiple messages", () => {
+ const input = {
+ tools: [
+ {
+ type: "function",
+ function: {
+ name: "tool_a",
+ description: "First tool",
+ parameters: {
+ type: "object",
+ properties: { param1: { type: "string" } },
+ required: ["param1"],
+ },
+ },
+ },
+ {
+ type: "function",
+ function: {
+ name: "tool_b",
+ description: "Second tool",
+ parameters: {
+ type: "object",
+ properties: { param2: { type: "string" } },
+ required: ["param2"],
+ },
+ },
+ },
+ ],
+ messages: [
+ { role: "system", content: "You are a helpful assistant" },
+ { role: "user", content: "test query" },
+ ],
+ };
+
+ const result = normalizeInput(input, { framework: "openai" });
+ expect(result.success).toBe(true);
+
+ // Tools should be attached to ALL messages
+ expect(result.data).toHaveLength(2);
+ expect(result.data?.[0].tools).toHaveLength(2);
+ expect(result.data?.[1].tools).toHaveLength(2);
+
+ // Check tool definitions are properly flattened
+ expect(result.data?.[0].tools?.[0].name).toBe("tool_a");
+ expect(result.data?.[0].tools?.[0].description).toBe("First tool");
+ expect(result.data?.[0].tools?.[0].parameters).toBeDefined();
+
+ expect(result.data?.[0].tools?.[1].name).toBe("tool_b");
+ expect(result.data?.[0].tools?.[1].description).toBe("Second tool");
+ });
+
it("should handle response format with tool calls", () => {
const output = {
role: "assistant",