Skip to content

fix(agents): preserve malformed function-call arguments instead of silent {} replacement - #61956

Merged
jalehman merged 3 commits into
openclaw:mainfrom
electricsheephq:fix/61478-preserve-malformed-args-v2
Apr 10, 2026
Merged

jalehman merged 3 commits into
openclaw:mainfrom
electricsheephq:fix/61478-preserve-malformed-args-v2

Conversation

@100yenadmin

@100yenadmin 100yenadmin commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #61478.

Recreated from #61527 with single-concern scope (as requested by @steipete).

What this fixes (plain English)

When replaying OpenAI assistant history, if a tool call's arguments contain malformed JSON, the code silently replaced them with an empty object {}. This caused irrecoverable data loss — the original arguments were permanently destroyed, making it impossible to debug or retry the tool call. The same data loss occurred through the HTTP/WS transport replay path, where preserved raw strings were double-encoded by JSON.stringify. The same coercion gap existed on the Anthropic and Google transport boundaries, where replayed string arguments would reach provider SDKs that expect a parsed object.

Technical details

Root cause: In buildAssistantMessageFromResponse(), the JSON.parse(item.arguments) catch block returned {} as Record<string, unknown> instead of preserving the raw string. Additionally, createOpenAIResponsesTransportStreamFn blindly JSON.stringify'd tool-call arguments, double-encoding any preserved raw strings. The Anthropic and Google transport stream builders both passed raw tool-call arguments straight into provider SDK payloads, so a preserved string would reach those providers un-coerced.

Fix:

  1. Catch block now returns item.arguments (the raw string) with a type assertion. Downstream convertMessagesToInputItems already handles typeof block.arguments === "string" explicitly.
  2. OpenAI transport stream serializer uses a typeof guard to avoid double-encoding.
  3. New shared helper coerceTransportToolCallArguments in src/agents/transport-stream-shared.ts parses raw string arguments back into objects at the Anthropic and Google transport boundaries (the providers that need parsed JSON), falling back to {} only if parsing fails — preserving the debug path for OpenAI while keeping other providers working.

Files changed

OpenAI path (commit 1)

  • src/agents/openai-ws-message-conversion.ts — catch block preserves raw arguments
  • src/agents/openai-transport-stream.ts — typeof guard prevents double-encoding string arguments
  • src/agents/openai-ws-stream.test.ts — regression tests for parse preservation + e2e replay round-trip
  • src/agents/openai-transport-stream.test.ts — regression test for transport stream string argument serialization

Cross-provider replay coercion (commit 2)

  • src/agents/transport-stream-shared.ts — new coerceTransportToolCallArguments helper
  • src/agents/anthropic-transport-stream.ts — coerces replayed string arguments at the Anthropic boundary
  • src/agents/anthropic-transport-stream.test.ts — regression tests covering string/object/malformed argument replay
  • src/agents/google-transport-stream.ts — coerces replayed string arguments at the Google boundary
  • src/agents/google-transport-stream.test.ts — regression tests covering string/object/malformed argument replay

Related

Test plan

  • Malformed JSON arguments preserved as raw string (buildAssistantMessageFromResponse)
  • Raw string arguments survive replay round-trip (convertMessagesToInputItems)
  • OpenAI transport stream serializes string arguments without double-encoding
  • Anthropic transport stream coerces replayed string arguments to objects
  • Google transport stream coerces replayed string arguments to objects
  • Valid JSON arguments still parsed normally across all transports

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a silent data-loss bug in buildAssistantMessageFromResponse: malformed function-call arguments were discarded and replaced with {} instead of being preserved. The one-line fix returns the raw item.arguments string, which the downstream convertMessagesToInputItems already handles via its typeof block.arguments === "string" branch (line 402 of the same file). The regression test is correctly scoped and uses the project-standard gpt-5.4 model constant.

Confidence Score: 5/5

Safe to merge — minimal, targeted fix with no side effects on well-covered downstream paths.

Single-line behavioral change that fixes a clear data-loss bug. The downstream consumer already has the typeof string guard for this case, no other consumers of toolCall arguments blindly treat them as objects, and the regression test is present and correct.

No files require special attention.

Reviews (1): Last reviewed commit: "fix(agents): preserve malformed function..." | Re-trigger Greptile

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a silent data-loss bug in the OpenAI WS response → stored assistant-message conversion path by preserving malformed tool-call argument payloads (instead of replacing them with {}), enabling faithful replay/debugging of tool calls.

Changes:

  • Preserve raw function_call.arguments string when JSON.parse fails in buildAssistantMessageFromResponse().
  • Add a regression unit test that asserts malformed JSON arguments are preserved.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/agents/openai-ws-message-conversion.ts Preserve raw tool-call arguments on JSON parse failure to avoid silent {} replacement/data loss.
src/agents/openai-ws-stream.test.ts Adds regression coverage for malformed tool-call argument preservation in parsed assistant messages.

Comment thread src/agents/openai-ws-stream.test.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6c7818421

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment thread src/agents/openai-transport-stream.ts Resolved
Comment thread src/agents/openai-transport-stream.ts Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f47f46b7a1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@100yenadmin

100yenadmin commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor Author

@steipete @jalehman confirmed from testing today. Makes GPT 5.4 less "cave man" dumb and confused 😵‍💫 when using tools.

@100yenadmin

Copy link
Copy Markdown
Contributor Author

Refreshed this branch on current main with the focused malformed-args fix. The branch now preserves raw malformed OpenAI function-call argument strings instead of collapsing them to {}, and it preserves those strings through the OpenAI replay serializer without double-encoding. I also reran the targeted agent regressions: pnpm exec node scripts/run-vitest.mjs run --config vitest.agents.config.ts src/agents/openai-ws-stream.test.ts src/agents/openai-transport-stream.test.ts (2 files, 146 tests passed).

I have not changed the broader cross-provider replay semantics here yet. Anthropic/Google still expect object-shaped args on replay, so if we want to harden malformed OpenAI history across providers too, that should land as a follow-up rather than getting mixed into this focused upstream bug fix.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment thread src/agents/transport-stream-shared.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

@jalehman jalehman self-assigned this Apr 10, 2026
@jalehman
jalehman merged commit 71bd9e0 into openclaw:main Apr 10, 2026
7 checks passed
@jalehman

Copy link
Copy Markdown
Contributor

Merged via squash.

Thanks @100yenadmin!

lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…lent {} replacement (openclaw#61956)

Merged via squash.

Prepared head SHA: 4185913
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…lent {} replacement (openclaw#61956)

Merged via squash.

Prepared head SHA: 4185913
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
globalcaos pushed a commit to globalcaos/tinkerclaw that referenced this pull request May 13, 2026
…lent {} replacement (openclaw#61956)

Merged via squash.

Prepared head SHA: 4185913
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…lent {} replacement (openclaw#61956)

Merged via squash.

Prepared head SHA: 4185913
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…lent {} replacement (openclaw#61956)

Merged via squash.

Prepared head SHA: 4185913
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
vixco pushed a commit to vixco/openclaw that referenced this pull request Aug 16, 2026
…lent {} replacement (openclaw#61956)

Merged via squash.

Prepared head SHA: 4185913
Co-authored-by: 100yenadmin <[email protected]>
Co-authored-by: jalehman <[email protected]>
Reviewed-by: @jalehman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(agents): OpenAI function-call replay silently replaces malformed arguments with {} — silent data loss on tool replay

3 participants