The Open Framework for Building Agents
fix(eve): parse tool-call arguments that arrive as a raw JSON string (#1240)
Rui7e4f7ef74afd
diff --git a/.changeset/tool-call-string-input.md b/.changeset/tool-call-string-input.mdnew file mode 100644index 000000000..580f2309f--- /dev/null+++ b/.changeset/tool-call-string-input.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Tool calls whose arguments arrive as a raw JSON string (e.g. provider-executed server tools) are now parsed, and arguments that cannot be parsed at all — such as malformed JSON emitted by the model — surface as a failed tool result the model can react to instead of failing the whole turn.diff --git a/packages/eve/src/harness/emission.test.ts b/packages/eve/src/harness/emission.test.tsindex ce1cdb4e1..4412593c0 100644--- a/packages/eve/src/harness/emission.test.ts+++ b/packages/eve/src/harness/emission.test.ts@@ -597,6 +597,54 @@ describe("emitStreamContent action requests", () => {},]);});++ it("turns malformed provider-executed tool call input into a failed tool result", async () => {+ const emit = createEmitStub();+ const message =+ 'Failed to parse tool-call arguments for "web_search" (call-bad): Expected a JSON-serializable object.';++ const result = await emitStreamContent(+ emit,+ EMISSION_STATE,+ streamOf([+ {+ // Opus 5 has emitted syntactically invalid JSON for provider+ // web_search arguments: an unquoted bare value.+ input: '{"objective": "Find the champion.", "search_queries": 2025 NBA Finals}',+ providerExecuted: true,+ toolCallId: "call-bad",+ toolName: "web_search",+ type: "tool-call",+ },+ { finishReason: "tool-calls", type: "finish-step" },+ ] as TextStreamPart<ToolSet>[]),+ {+ excludedActionToolNames: new Set(),+ tools: new Map<string, HarnessToolDefinition>(),+ },+ );++ const events = vi.mocked(emit).mock.calls.map(([event]) => event);+ expect(events).toEqual([+ expect.objectContaining({+ data: expect.objectContaining({+ error: { code: "ACTION_RESULT_FAILED", message },+ result: {+ callId: "call-bad",+ isError: true,+ kind: "tool-result",+ output: message,+ toolName: "web_search",+ },+ status: "failed",+ }),+ type: "action.result",+ }),+ ]);+ expect([...result.invalidInputToolCallIds]).toEqual(["call-bad"]);+ // Provider results live in the assistant response; no local tool message.+ expect(result.trailingInlineToolResultParts).toEqual([]);+ });});describe("emitStreamContent error-part handling", () => {diff --git a/packages/eve/src/harness/emission.ts b/packages/eve/src/harness/emission.tsindex 69197f0e1..0d8bb4b24 100644--- a/packages/eve/src/harness/emission.ts+++ b/packages/eve/src/harness/emission.ts@@ -37,13 +37,11 @@ import {createRuntimeToolResultFromToolError,createToolResultMessagePartFromToolError,} from "#harness/action-result-helpers.js";-import {- createRuntimeActionRequestFromToolCall,- resolveToolCallInputObject,-} from "#harness/runtime-actions.js";+import { createRuntimeActionRequestFromToolCall } from "#harness/runtime-actions.js";import {createInvalidToolCallInputError,isInvalidToolCall,+ resolveProviderToolCallRequest,} from "#harness/tool-call-input-errors.js";import type {RuntimeActionRequest,@@ -455,15 +453,15 @@ async function consumeStreamContent(await flushCurrentMessage();}- providerActionBatch.observe({- callId: toolCall.toolCallId,- input: resolveToolCallInputObject(toolCall.input, {- callId: toolCall.toolCallId,- toolName: toolCall.toolName,- }),- kind: "tool-call",- toolName: toolCall.toolName,- });+ const resolved = resolveProviderToolCallRequest(toolCall);+ if (resolved.toolError !== undefined) {+ invalidInputToolCallIds.add(toolCall.toolCallId);+ await emitActionResult(createRuntimeToolResultFromToolError(resolved.toolError));+ handledInlineToolResultCallIds.add(toolCall.toolCallId);+ return;+ }++ providerActionBatch.observe(resolved.request);};const emitActionResult = async (result: RuntimeToolResultActionResult): Promise<void> => {diff --git a/packages/eve/src/harness/input-requests.ts b/packages/eve/src/harness/input-requests.tsindex af55b521f..7d412e222 100644--- a/packages/eve/src/harness/input-requests.ts+++ b/packages/eve/src/harness/input-requests.ts@@ -6,8 +6,8 @@ import type {} from "#runtime/actions/types.js";import type { InputRequest, InputResponse } from "#runtime/input/types.js";import { resolveTextToResponses } from "#channel/resolve-text.js";-import { parseJsonObject, type JsonObject } from "#shared/json.js";import { coalesceTurnInputs } from "#harness/messages.js";+import { resolveToolCallInputObject } from "#harness/runtime-actions.js";import {isSessionLimitContinuationRequest,resolveSessionLimitContinuation,@@ -676,22 +676,3 @@ export function createRuntimeToolCallActionFromToolCall(input: {toolName: input.toolCall.toolName,};}--function resolveToolCallInputObject(- value: unknown,- context: { readonly callId: string; readonly toolName: string },-): JsonObject {- if (value === undefined || value === null) {- return {};- }-- try {- return parseJsonObject(value);- } catch (error) {- const detail = error instanceof Error ? error.message : String(error);- throw new TypeError(- `Failed to parse tool-call arguments for "${context.toolName}" (${context.callId}): ${detail}`,- { cause: error },- );- }-}diff --git a/packages/eve/src/harness/runtime-actions.test.ts b/packages/eve/src/harness/runtime-actions.test.tsindex 593a0baa5..ce74af0ea 100644--- a/packages/eve/src/harness/runtime-actions.test.ts+++ b/packages/eve/src/harness/runtime-actions.test.ts@@ -4,6 +4,7 @@ import {getPendingRuntimeActionBatch,recordPendingSubagentChild,resolvePendingRuntimeActions,+ resolveToolCallInputObject,setPendingRuntimeActionBatch,} from "#harness/runtime-actions.js";import { getSessionTokenUsage, setTurnUsageState } from "#harness/turn-tag-state.js";@@ -135,3 +136,38 @@ describe("pending subagent child adoption", () => {});});});++describe("resolveToolCallInputObject", () => {+ const context = { callId: "call-1", toolName: "web_search" };++ it("passes plain objects through", () => {+ expect(resolveToolCallInputObject({ query: "eve" }, context)).toEqual({ query: "eve" });+ });++ it("treats undefined, null, and empty-string inputs as empty arguments", () => {+ expect(resolveToolCallInputObject(undefined, context)).toEqual({});+ expect(resolveToolCallInputObject(null, context)).toEqual({});+ expect(resolveToolCallInputObject("", context)).toEqual({});+ expect(resolveToolCallInputObject(" ", context)).toEqual({});+ });++ it("parses raw JSON-string inputs from provider-executed tool calls", () => {+ expect(resolveToolCallInputObject('{"query":"eve"}', context)).toEqual({ query: "eve" });+ });++ it("rejects strings that are not JSON objects, naming the tool and call", () => {+ expect(() => resolveToolCallInputObject('"query"', context)).toThrow(+ /web_search.*call-1.*Expected a JSON-serializable object/su,+ );+ expect(() => resolveToolCallInputObject("not json", context)).toThrow(/web_search.*call-1/su);+ });++ it("rejects non-object JSON values", () => {+ expect(() => resolveToolCallInputObject(42, context)).toThrow(+ /Expected a JSON-serializable object/u,+ );+ expect(() => resolveToolCallInputObject(["a"], context)).toThrow(+ /Expected a JSON-serializable object/u,+ );+ });+});diff --git a/packages/eve/src/harness/runtime-actions.ts b/packages/eve/src/harness/runtime-actions.tsindex 902f32424..95d853829 100644--- a/packages/eve/src/harness/runtime-actions.ts+++ b/packages/eve/src/harness/runtime-actions.ts@@ -420,6 +420,10 @@ export function createRuntimeActionRequestFromToolCall(input: {* Coerces an AI SDK tool-call `input` into the runtime-action `JsonObject`* contract, throwing a `TypeError` (with the original as `cause`) that names* the offending tool when the payload is not a JSON object.+ *+ * String inputs are parsed as JSON first: the model protocol carries tool+ * arguments as text, and provider-executed tool calls can surface that raw+ * string — or an empty string when the model sends no arguments.*/export function resolveToolCallInputObject(value: unknown,@@ -429,8 +433,12 @@ export function resolveToolCallInputObject(return {};}+ if (typeof value === "string" && value.trim() === "") {+ return {};+ }+try {- return parseJsonObject(value);+ return parseJsonObject(typeof value === "string" ? parseJsonStringInput(value) : value);} catch (error) {// This module is bundled into the workflow driver body, which cannot// import the logger, so enrich the error (and keep the original as@@ -443,6 +451,16 @@ export function resolveToolCallInputObject(}}+function parseJsonStringInput(value: string): unknown {+ try {+ return JSON.parse(value);+ } catch {+ // Not JSON at all — return the raw string so parseJsonObject rejects it+ // with the canonical "Expected a JSON-serializable object." detail.+ return value;+ }+}+function toToolResultOutput(result: RuntimeActionResult): ToolResultPart["output"] {if (typeof result.output === "string") {if (result.isError === true) {diff --git a/packages/eve/src/harness/tool-call-input-errors.ts b/packages/eve/src/harness/tool-call-input-errors.tsindex d6a3f199f..40a93345b 100644--- a/packages/eve/src/harness/tool-call-input-errors.ts+++ b/packages/eve/src/harness/tool-call-input-errors.ts@@ -1,5 +1,6 @@import type { ToolSet, TypedToolCall, TypedToolError } from "ai";+import type { RuntimeToolCallActionRequest } from "#runtime/actions/types.js";import { resolveToolCallInputObject } from "#harness/runtime-actions.js";/**@@ -56,6 +57,52 @@ export function createInvalidToolCallInputError(input: {return toolError as TypedToolError<ToolSet>;}+/**+ * Resolves a provider-executed tool call into an observable runtime-action+ * request, or the synthesized tool error when the model emitted arguments+ * that cannot satisfy the JSON-object contract.+ *+ * Provider-executed calls skip the AI SDK's input validation, so malformed+ * JSON argument text reaches the harness only here.+ */+export function resolveProviderToolCallRequest(toolCall: {+ readonly input?: unknown;+ readonly toolCallId: string;+ readonly toolName: string;+}):+ | { readonly request: RuntimeToolCallActionRequest; readonly toolError?: undefined }+ | { readonly request?: undefined; readonly toolError: TypedToolError<ToolSet> } {+ try {+ return {+ request: {+ callId: toolCall.toolCallId,+ input: resolveToolCallInputObject(toolCall.input, {+ callId: toolCall.toolCallId,+ toolName: toolCall.toolName,+ }),+ kind: "tool-call",+ toolName: toolCall.toolName,+ },+ };+ } catch (error) {+ if (!(error instanceof TypeError)) {+ throw error;+ }+ return {+ toolError: createInvalidToolCallInputError({+ error,+ toolCall: {+ input: toolCall.input,+ providerExecuted: true,+ toolCallId: toolCall.toolCallId,+ toolName: toolCall.toolName,+ type: "tool-call",+ } as TypedToolCall<ToolSet>,+ }),+ };+ }+}+export function getInvalidToolCallInputError(input: {readonly toolCall: TypedToolCall<ToolSet>;}): TypedToolError<ToolSet> | undefined {