Skip to content

fix(agents): correct chat agent example for tool approval and coercion#31278

Open
thomasgauvin wants to merge 1 commit into
productionfrom
fix-chat-agent-example-docs
Open

fix(agents): correct chat agent example for tool approval and coercion#31278
thomasgauvin wants to merge 1 commit into
productionfrom
fix-chat-agent-example-docs

Conversation

@thomasgauvin
Copy link
Copy Markdown
Contributor

Summary

The chat agent example at /agents/examples/chat-agent/ contains 4 bugs that prevent the approval flow and tool execution from working correctly.

Issues Found

1. Zod number validation fails with LLM string outputs (line 133-134)

Problem: z.number() rejects string values like "4" and "23" that LLMs output when calling tools.
Fix: Use z.coerce.number() to automatically convert strings to numbers.

2. Tool part type is dynamic, not "tool" (line 229)

Problem: part.type is set to dynamic values like "tool-calculate", not the literal string "tool". The condition part.type === "tool" never matches.
Fix: Check part.state === "approval-requested" directly instead of coupling type and state checks.

3. Wrong approval state name (line 230)

Problem: The state is "approval-requested", not "approval-required". The UI never renders.
Fix: Use the correct state name "approval-requested".

4. Wrong ID passed to addToolApprovalResponse (line 241, 251)

Problem: addToolApprovalResponse expects the approval.id (from part.approval.id), not part.toolCallId. Passing toolCallId causes the warning: "Could not find toolCallId for approval ID" and the server never gets notified.
Fix: Use getToolApproval(part).id as documented in the API reference.

5. Also fixed same z.number() issue in chat-agents API reference (line 1006)

The main /agents/communication-channels/chat/chat-agents/ page had the same Zod schema issue in its processPayment example.

Files Changed

  • src/content/docs/agents/examples/chat-agent.mdx — Full example fix
  • src/content/docs/agents/communication-channels/chat/chat-agents.mdx — API reference z.coerce.number() fix

Verification

These changes have been tested and verified to resolve the MissingToolResultsError and make the approval UI render correctly.

@cloudflare-docs-bot
Copy link
Copy Markdown
Contributor

cloudflare-docs-bot Bot commented Jun 7, 2026

Review

✅ No style-guide issues found in commit ffcf821.

Commands

Only codeowners can run commands. Post a comment with the command to trigger it.

Command Description
/review Runs a review now. Incremental if a prior review exists, full if not.
/full-review Re-reviews the entire PR diff from scratch, ignoring incremental history. Useful after a rebase, when you want a fresh review, or if the bot gets out of sync and reports issues that no longer exist.

Copy link
Copy Markdown
Contributor

@ask-bonk ask-bonk Bot left a comment

Choose a reason for hiding this comment

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

Cannot merge — 2 blocking issues need fixing.

CRITICAL: This PR accidentally commits ~14,600 macOS resource fork files (._* AppleDouble files) across the repository. These must be removed before merge. Check your local Git configuration to prevent this — for example, git config --global core.precomposeunicode true and ensure your global .gitignore excludes ._* and .DS_Store.

HIGH: The branch contains an unrelated commit (7205f4fe) that adds a Workers React + Vite deployment guide. If this wasn't intentional, rebase the branch onto production and drop that commit so the PR stays scoped to the chat agent fixes.

The actual content changes to chat-agent.mdx and chat-agents.mdx look correct.

@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk Bot commented Jun 7, 2026

Done. Here's a summary of the review for PR #31278:

Verdict: Changes requested — 2 blocking issues prevent merge.

I applied the labels documentation and bug to the PR.

Critical Issues Flagged

  1. ._* Resource Fork Files (CRITICAL)
    The commit 0d94003a accidentally added ~14,600 macOS resource fork files (._* AppleDouble files) across the entire repository. These binary files bloat the repo and are the likely cause of the CI failure. I recommended removing them and fixing local Git configuration to prevent recurrence.

  2. Unrelated Commit in Branch (HIGH)
    The PR branch also contains commit 7205f4fe ("[Workers] Add existing project deployment guide for React + Vite"), which is not mentioned in the PR description and is out of scope. I recommended rebasing onto production and dropping that commit if it was unintentional.

  3. CI Build Failure (MEDIUM)
    The build failed in CI. Once the accidental files are removed, the author should verify the build passes.

Technical Assessment of Intended Changes

The actual documentation fixes in chat-agent.mdx and chat-agents.mdx are correct:

  • Using z.coerce.number() properly handles LLM string outputs.
  • Checking part.state === "approval-requested" is the right state name.
  • Using getToolApproval(part).id for addToolApprovalResponse matches the API.

No inline suggestions were needed for the MDX content itself because the fixes are technically accurate and properly formatted.

github run

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 7, 2026

This pull request requires reviews from CODEOWNERS as it changes files that match the following patterns:

Pattern Owners
/src/content/docs/agents/ @irvinebroque, @rita3ko, @elithrar, @thomasgauvin, @threepointone, @whoiskatrin, @cloudflare/product-owners, @cloudflare/ai-agents, @cloudflare/dev-plat-leads

@thomasgauvin
Copy link
Copy Markdown
Contributor Author

/bonk rereview

Copy link
Copy Markdown
Contributor

@ask-bonk ask-bonk Bot left a comment

Choose a reason for hiding this comment

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

"Both blocking issues from the previous review are now resolved.\n\n- No macOS resource fork files (._*) remain in the branch.\n- The unrelated commit has been removed; the branch is scoped to the chat agent fixes only.\n\nThe content changes are all technically correct:\n\n- z.coerce.number() properly handles string values that LLMs emit for numeric tool arguments.\n- part.state === \"approval-requested\" matches the actual runtime state name.\n- getToolApproval(part).id is the correct identifier to pass to addToolApprovalResponse.\n- Removing the part.type === \"tool\" checks is appropriate since the part type is dynamic (tool-*).\n\nApproved.\n"

@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk Bot commented Jun 7, 2026

Posted an approving review on PR #31278.

The previous blocking issues are both resolved:

  • Resource fork files: None remain in the branch.
  • Unrelated commit: Removed; the branch is now scoped to the chat agent fixes only.

The fixes in chat-agent.mdx and chat-agents.mdx are all technically correct:

Fix Location
z.coerce.number() for LLM string outputs chat-agent.mdx lines 133–134, chat-agents.mdx line 1006
Correct state name "approval-requested" chat-agent.mdx line 228
Correct getToolApproval(part).id for addToolApprovalResponse chat-agent.mdx lines 240, 250
Remove incorrect part.type === "tool" checks chat-agent.mdx lines 228, 262

github run

@thomasgauvin
Copy link
Copy Markdown
Contributor Author

/bonk rereview

Copy link
Copy Markdown
Contributor

@ask-bonk ask-bonk Bot left a comment

Choose a reason for hiding this comment

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

Re-reviewed — no issues found. The fixes are all correct:

  • properly handles LLM string outputs for numeric tool arguments.
  • matches the actual runtime state name.
  • is the correct identifier for .
  • Removing the checks is appropriate since the part type is dynamic.

Approved.

@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk Bot commented Jun 7, 2026

Re-reviewed — no issues found. The fixes are all correct:

  • z.coerce.number() properly handles LLM string outputs for numeric tool arguments.
  • part.state === "approval-requested" matches the actual runtime state name.
  • getToolApproval(part).id is the correct identifier for addToolApprovalResponse.
  • Removing the part.type === "tool" checks is appropriate since the part type is dynamic.

Approved.

@ask-bonk
Copy link
Copy Markdown
Contributor

ask-bonk Bot commented Jun 7, 2026

Re-reviewed PR #31278 and posted an approving review.

The fixes are all technically correct and properly scoped:

  • z.coerce.number() in both files properly handles LLM string outputs for numeric tool arguments.
  • part.state === "approval-requested" matches the actual runtime state name.
  • getToolApproval(part).id is the correct identifier for addToolApprovalResponse.
  • Removing the part.type === "tool" checks is appropriate since the part type is dynamic (tool-*).

No blocking issues or style-guide violations found in the current diff.

github run

@thomasgauvin
Copy link
Copy Markdown
Contributor Author

Manual review. LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working documentation Documentation edits product:changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.