Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(llm-proxy): read empty arguments on a closed tool call as no parameters (#739)

Alfredo Lopezcbf740a7f0ba
diff --git a/codex-llm-proxy/src/codexTransport.ts b/codex-llm-proxy/src/codexTransport.ts
index dd9193897..319d9274e 100644
--- a/codex-llm-proxy/src/codexTransport.ts
+++ b/codex-llm-proxy/src/codexTransport.ts
@@ -451,7 +451,7 @@ async function consumeSse(
}
for (const call of pending.values()) {
if (call.emitted) continue
- const args = parseToolArguments(call.arguments)
+ const args = parseToolArguments(call.arguments, { closed: false })
await acceptFrame({ type: 'tool_call', id: call.id, name: call.name, arguments: args })
call.emitted = true
}
@@ -539,7 +539,7 @@ function mapUpstreamEvent(
row.item.type === 'function_call'
) {
const call = upsertPendingTool(pending, row.item)
- if (isCompleteJson(call.arguments)) return { frame: emitToolCall(call) }
+ if (isCompleteJson(call.arguments)) return { frame: emitToolCall(call, { closed: false }) }
return {}
}
if (type === 'response.function_call_arguments.delta') {
@@ -559,7 +559,7 @@ function mapUpstreamEvent(
) {
const source = isPlainObject(row.item) ? row.item : row
const call = upsertPendingTool(pending, source)
- if (call && !call.emitted) return { frame: emitToolCall(call) }
+ if (call && !call.emitted) return { frame: emitToolCall(call, { closed: true }) }
return {}
}
if (type === 'response.completed') {
@@ -601,9 +601,13 @@ function upsertPendingTool(
if (typeof source.name === 'string' && source.name.trim()) current.name = source.name
const rawArgs = source.arguments
if (typeof rawArgs === 'string') {
+ // A closing event with empty or whitespace-only arguments carries nothing
+ // new; it must not replace the buffer the deltas built, truncated or not.
current.arguments = source.append
? `${current.arguments}${rawArgs}`
- : rawArgs || current.arguments
+ : rawArgs.trim()
+ ? rawArgs
+ : current.arguments
} else if (isPlainObject(rawArgs) && !source.append) {
current.arguments = JSON.stringify(rawArgs)
}
@@ -611,13 +615,15 @@ function upsertPendingTool(
return current
}
-function emitToolCall(call: PendingToolCall): StreamFrame {
+// `closed` is true only when the upstream closed the item with
+// `response.output_item.done` or `response.function_call_arguments.done`.
+function emitToolCall(call: PendingToolCall, options: { closed: boolean }): StreamFrame {
call.emitted = true
return {
type: 'tool_call',
id: call.id,
name: call.name,
- arguments: parseToolArguments(call.arguments),
+ arguments: parseToolArguments(call.arguments, options),
}
}
@@ -633,10 +639,14 @@ function isCompleteJson(raw: string): boolean {
}
// A call runs with exactly the arguments the model produced. Arguments that are
-// not a JSON object (truncated, a non-object value, or empty) refuse the whole
-// response instead of executing the tool with `{}`; a call without parameters
-// arrives as the string "{}".
-function parseToolArguments(raw: string): Record<string, unknown> {
+// not a JSON object (truncated or a non-object value) refuse the whole response
+// instead of executing the tool with `{}`. Empty or whitespace-only arguments
+// on a call the upstream closed are a call without parameters, read as `{}`;
+// the Host still validates `{}` against the tool's schema. Empty arguments on
+// a call that was never closed are refused, because nothing says the call was
+// complete.
+function parseToolArguments(raw: string, options: { closed: boolean }): Record<string, unknown> {
+ if (options.closed && raw.trim() === '') return {}
let parsed: unknown
try {
parsed = JSON.parse(raw)
diff --git a/docs/architecture/codex-subscription-transport-contract.md b/docs/architecture/codex-subscription-transport-contract.md
index a3baf1b44..a836bf7b8 100644
--- a/docs/architecture/codex-subscription-transport-contract.md
+++ b/docs/architecture/codex-subscription-transport-contract.md
@@ -380,12 +380,17 @@ Stable codes: `insufficient_scope`, `no_grant`, `model_not_allowed`,
failover-eligible (failover class `null`), so the task fails with that code
instead of `LLM_MODEL_OVERLOADED`.
- `invalid_tool_arguments`: a tool call's `arguments` are not a JSON object —
- truncated JSON, a non-object value, or an empty string (a call without
- parameters arrives as `"{}"`). The transport refuses the whole response
- instead of running the tool with `{}`, both when the call is closed by
- `response.output_item.done` / `response.function_call_arguments.done` and
- when a pending call is flushed at `response.completed`. A stream that was
- canceled or that the upstream failed keeps its own outcome (`canceled`,
+ truncated JSON or a non-object value. The transport refuses the whole
+ response instead of running the tool with `{}`, both when the call is closed
+ by `response.output_item.done` / `response.function_call_arguments.done` and
+ when a pending call is flushed at `response.completed`. Empty or
+ whitespace-only `arguments` on a call closed by one of those two events are
+ a call without parameters and reach the Host as `{}`; the Host still
+ validates `{}` against the tool's schema. A closing event with empty
+ `arguments` never replaces what the deltas already delivered, so truncated
+ deltas stay refused. Empty `arguments` on a call that was never closed,
+ flushed at `response.completed`, are refused like truncated JSON. A stream
+ that was canceled or that the upstream failed keeps its own outcome (`canceled`,
`context_length_exceeded` or `provider_unavailable`), because its open call
is truncated as a consequence. Delivered like `tool_call_limit_exceeded` —
422 carrying the code, or an SSE error frame once text is on the wire. The
diff --git a/docs/architecture/grok-subscription-transport-contract.md b/docs/architecture/grok-subscription-transport-contract.md
index 18f52b6df..d650778ef 100644
--- a/docs/architecture/grok-subscription-transport-contract.md
+++ b/docs/architecture/grok-subscription-transport-contract.md
@@ -281,11 +281,17 @@ Stable codes: `insufficient_scope`, `no_grant`, `model_not_allowed`,
request-size work classifies this refusal as context length (above) and
leaves the bound's value and the wording as they are.
- `invalid_tool_arguments`: a tool call's `arguments` are not a JSON object —
- truncated JSON, a non-object value, or an empty string (a call without
- parameters arrives as `"{}"`). The transport refuses the whole response
- instead of running the tool with `{}`, both when the call is closed by
- `response.output_item.done` / `response.function_call_arguments.done` and
- when a pending call is flushed at `response.completed`. A stream that was
+ truncated JSON or a non-object value. The transport refuses the whole
+ response instead of running the tool with `{}`, both when the call is closed
+ by `response.output_item.done` / `response.function_call_arguments.done` and
+ when a pending call is flushed at `response.completed`. Empty or
+ whitespace-only `arguments` on a call closed by one of those two events are
+ a call without parameters and reach the Host as `{}`; the Host still
+ validates `{}` against the tool's schema. A closing event with empty
+ `arguments` never replaces what the deltas already delivered, so truncated
+ deltas stay refused. Empty `arguments` on a call that was never closed,
+ flushed at `response.completed`, are refused like truncated JSON. A stream
+ that was
canceled or that the upstream failed keeps its own outcome (`canceled`,
`provider_unavailable`), because its open call is truncated as a
consequence. Delivered like `tool_call_limit_exceeded` — 422 carrying the
diff --git a/grok-llm-proxy/src/grokTransport.ts b/grok-llm-proxy/src/grokTransport.ts
index 609f45994..217e340ad 100644
--- a/grok-llm-proxy/src/grokTransport.ts
+++ b/grok-llm-proxy/src/grokTransport.ts
@@ -473,7 +473,7 @@ async function consumeSse(
}
for (const call of pending.values()) {
if (call.emitted) continue
- const args = parseToolArguments(call.arguments)
+ const args = parseToolArguments(call.arguments, { closed: false })
await acceptFrame({ type: 'tool_call', id: call.id, name: call.name, arguments: args })
call.emitted = true
}
@@ -565,7 +565,7 @@ function mapUpstreamEvent(
row.item.type === 'function_call'
) {
const call = upsertPendingTool(pending, row.item, budget)
- if (isCompleteJson(call.arguments)) return { frame: emitToolCall(call) }
+ if (isCompleteJson(call.arguments)) return { frame: emitToolCall(call, { closed: false }) }
return {}
}
if (type === 'response.function_call_arguments.delta') {
@@ -589,7 +589,7 @@ function mapUpstreamEvent(
) {
const source = isPlainObject(row.item) ? row.item : row
const call = upsertPendingTool(pending, source, budget)
- if (call && !call.emitted) return { frame: emitToolCall(call) }
+ if (call && !call.emitted) return { frame: emitToolCall(call, { closed: true }) }
return {}
}
if (type === 'response.completed') {
@@ -621,9 +621,13 @@ function upsertPendingTool(
const rawArgs = source.arguments
const retainedBefore = current.arguments.length
if (typeof rawArgs === 'string') {
+ // A closing event with empty or whitespace-only arguments carries nothing
+ // new; it must not replace the buffer the deltas built, truncated or not.
current.arguments = source.append
? `${current.arguments}${rawArgs}`
- : rawArgs || current.arguments
+ : rawArgs.trim()
+ ? rawArgs
+ : current.arguments
} else if (isPlainObject(rawArgs) && !source.append) {
current.arguments = JSON.stringify(rawArgs)
}
@@ -639,13 +643,15 @@ function upsertPendingTool(
return current
}
-function emitToolCall(call: PendingToolCall): StreamFrame {
+// `closed` is true only when the upstream closed the item with
+// `response.output_item.done` or `response.function_call_arguments.done`.
+function emitToolCall(call: PendingToolCall, options: { closed: boolean }): StreamFrame {
call.emitted = true
return {
type: 'tool_call',
id: call.id,
name: call.name,
- arguments: parseToolArguments(call.arguments),
+ arguments: parseToolArguments(call.arguments, options),
}
}
@@ -661,10 +667,14 @@ function isCompleteJson(raw: string): boolean {
}
// A call runs with exactly the arguments the model produced. Arguments that are
-// not a JSON object (truncated, a non-object value, or empty) refuse the whole
-// response instead of executing the tool with `{}`; a call without parameters
-// arrives as the string "{}".
-function parseToolArguments(raw: string): Record<string, unknown> {
+// not a JSON object (truncated or a non-object value) refuse the whole response
+// instead of executing the tool with `{}`. Empty or whitespace-only arguments
+// on a call the upstream closed are a call without parameters, read as `{}`;
+// the Host still validates `{}` against the tool's schema. Empty arguments on
+// a call that was never closed are refused, because nothing says the call was
+// complete.
+function parseToolArguments(raw: string, options: { closed: boolean }): Record<string, unknown> {
+ if (options.closed && raw.trim() === '') return {}
let parsed: unknown
try {
parsed = JSON.parse(raw)