Repository navigation
fix(tool_parser): ignore <|END_ACTION|> inside Cohere JSON strings - #1941
Conversation
A parameter value that mentions the end marker truncated the action JSON at the first match. Scan for END_ACTION only outside string literals. Signed-off-by: santhreal <64453045+santhreal@users.noreply.github.com>
|
Warning Review limit reached
Next review available in: 28 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughCohere action parsing now detects ChangesCohere action parsing
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request updates the Cohere tool parser to correctly handle and ignore END_ACTION markers when they appear inside JSON string parameters. It introduces a helper function find_end_action_outside_strings to parse the JSON structure and skip occurrences within strings, and adds a corresponding integration test. The review feedback suggests a performance optimization in the helper function to check the first byte of the needle before calling starts_with.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if bytes[i..].starts_with(needle) { | ||
| return Some(i); | ||
| } |
There was a problem hiding this comment.
To avoid calling starts_with on every single character outside of JSON strings, we can first check if the current byte matches the first byte of the needle (using needle.first()). This improves performance by avoiding slice comparison overhead for non-matching characters.
| if bytes[i..].starts_with(needle) { | |
| return Some(i); | |
| } | |
| if Some(&b) == needle.first() && bytes[i..].starts_with(needle) { | |
| return Some(i); | |
| } |
There was a problem hiding this comment.
Thanks. Added the needle.first() guard in 3e1c3a1.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1f881c4434
ℹ️ 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".
| if in_string { | ||
| if b == b'\\' { | ||
| escape = true; | ||
| i += 1; | ||
| continue; | ||
| } | ||
| if b == b'"' { | ||
| in_string = false; | ||
| } | ||
| i += 1; | ||
| continue; |
There was a problem hiding this comment.
Recover at delimiters after malformed quoted JSON
When model output has an unterminated JSON string before a real <|END_ACTION|> delimiter, in_string never clears, so this loop ignores that delimiter. parse_complete then leaves the entire action block in normal_text, while incremental parsing stays in InAction and can swallow all later chunks/actions; before this change the delimiter was still recognized and the malformed action was discarded. The existing malformed-JSON coverage shows malformed action output is an expected parser input.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks. Valid marker-in-string cases still use the string-aware scan; if quotes never close we now fall back to a plain END_ACTION search so malformed actions still terminate (3e1c3a1).
Keep string-aware scanning for valid marker-in-string payloads, but fall back to a plain delimiter search when quotes never close so malformed actions still terminate. Signed-off-by: santhreal <64453045+santhreal@users.noreply.github.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3e1c3a1ef3
ℹ️ 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".
| if in_string { | ||
| return s.find(END_ACTION); |
There was a problem hiding this comment.
Select the real delimiter after malformed strings
When malformed output has an unterminated JSON string containing <|END_ACTION|> before the actual closing delimiter, this fallback returns the first marker—the one still inside the string—rather than the later real delimiter. The parser then consumes that marker, drops the malformed action, and leaves the real delimiter in normal text (or disrupts subsequent streaming state), so the documented malformed-JSON recovery does not work for the same marker-in-string case this change introduces.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks. Fallback now uses rfind so an earlier marker inside the unclosed string is skipped in favor of the last delimiter (2ce7c0a), with a regression test.
An earlier marker can sit inside the unclosed string; the real close is usually last. Signed-off-by: santhreal <64453045+santhreal@users.noreply.github.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2ce7c0a725
ℹ️ 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".
| if in_string { | ||
| // Prefer the last marker: an earlier one may sit inside the | ||
| // unclosed string before the real closing delimiter. | ||
| return s.rfind(END_ACTION); |
There was a problem hiding this comment.
Preserve markers inside unfinished streamed strings
When a valid string containing <|END_ACTION|> arrives before its closing quote and the real delimiter (a normal streaming chunk boundary), in_string remains true and this fallback returns that in-string marker. parse_incremental then drains it and switches to Text, so the rest of the JSON is emitted as normal text rather than completing the tool call once later chunks arrive. The fallback must not run for an incomplete stream without a confirmed closing delimiter.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks. Agreed — removed the find/rfind fallback entirely in $(git rev-parse --short HEAD). Unclosed quotes now return None so streaming can wait; only a delimiter outside strings completes the action.
| if in_string { | ||
| // Prefer the last marker: an earlier one may sit inside the | ||
| // unclosed string before the real closing delimiter. | ||
| return s.rfind(END_ACTION); |
There was a problem hiding this comment.
Do not consume subsequent actions during malformed recovery
For a malformed first action with an unclosed string followed by a valid second <|START_ACTION|>...<|END_ACTION|> block, rfind selects the second action's closing marker. extract_action_json consequently hands both blocks to one JSON parse, which fails, and consumes the valid second block instead of allowing the loop to parse it. This regresses recovery for valid actions following malformed model output.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Thanks. Same fix: no rfind recovery, so a malformed first action cannot consume a later valid block ($(git rev-parse --short HEAD)).
String-aware scan only. A fallback terminates mid-stream when a marker sits inside an unfinished string, and rfind can swallow a following action. Unclosed quotes wait for more input instead. Signed-off-by: santhreal <64453045+santhreal@users.noreply.github.com>
Description
Problem
Cohere tool calls are delimited by
<|START_ACTION|>…<|END_ACTION|>. A parameter value that literally mentions<|END_ACTION|>was truncated at the first marker match, so the action JSON became invalid and the tool call was dropped.Solution
Scan for
END_ACTIONonly outside JSON string literals (with escape awareness), matching how sibling parsers treat delimiter markers inside payloads.Changes
crates/tool_parser/src/parsers/cohere.rs: string-awareEND_ACTIONlocator for complete and streaming pathscrates/tool_parser/tests/tool_parser_cohere.rs: regression for a marker inside a string paramTest Plan
Before (pristine main): input
<|START_ACTION|>{"tool_name": "echo", "parameters": {"text": "say <|END_ACTION|> please"}}<|END_ACTION|>yields 0 tools (JSON cut mid-string).
After: 1 tool named
echowithtext == "say <|END_ACTION|> please".cargo +nightly fmt --all -- --check cargo clippy -p tool-parser --all-targets -- -D warnings cargo test -p tool-parser --test tool_parser_cohereChecklist
cargo +nightly fmtpassescargo clippy --all-targetsontool-parserwith-D warningspassesSummary by CodeRabbit
Bug Fixes
Tests