Repository navigation
fix(translator): coerce submit_pr_review functionalChanges/findings to arrays #2242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 3 commits into
cheaper-inference:release/v3.8.0
from
NomenAK:fix/translator-tool-call-shim-2026-05-14
May 14, 2026
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Defensive shims for tool calls whose strict-schema fields can be malformed | ||
| // by upstream models (e.g. MiMo emitting empty objects/strings instead of | ||
| // arrays for Capy's submit_pr_review). | ||
| // | ||
| // Applied on the assembled OpenAI tool-call arguments after streaming, just | ||
| // before they are re-emitted as a single Claude input_json_delta. | ||
| // | ||
| // To add a new shim: register a (input) => input transformer in TOOL_SHIMS | ||
| // keyed by the tool name. The transformer must accept arbitrary input and | ||
| // return a JSON-safe value. | ||
|
|
||
| type ShimFn = (input: unknown) => unknown; | ||
|
|
||
| function coerceToArray(v: unknown): unknown[] { | ||
| if (Array.isArray(v)) return v; | ||
| if (v == null) return []; | ||
| if (typeof v === "string") { | ||
| if (v === "") return []; | ||
| try { | ||
| const parsed = JSON.parse(v); | ||
| return Array.isArray(parsed) ? parsed : []; | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
| // Plain object or other non-array → empty | ||
| return []; | ||
| } | ||
|
|
||
| const TOOL_SHIMS: Record<string, ShimFn> = { | ||
| submit_pr_review: (input) => { | ||
| if (typeof input !== "object" || input === null || Array.isArray(input)) return input; | ||
| const patched = { ...(input as Record<string, unknown>) }; | ||
| for (const key of ["functionalChanges", "findings"]) { | ||
| patched[key] = coerceToArray(patched[key]); | ||
| } | ||
| return patched; | ||
| }, | ||
| }; | ||
|
|
||
| export function hasToolCallShim(name: string | undefined | null): boolean { | ||
| return typeof name === "string" && Object.prototype.hasOwnProperty.call(TOOL_SHIMS, name); | ||
| } | ||
|
|
||
| /** | ||
| * Apply the registered shim for a tool call's raw assembled arguments string. | ||
| * Returns a stringified JSON value safe to emit as input_json_delta.partial_json. | ||
| * If the buffer is unparseable, returns the empty-object JSON `{}` after applying | ||
| * the shim with `{}` as input (so required arrays still get injected). | ||
| */ | ||
| export function applyToolCallShimToBuffer(name: string, raw: string): string { | ||
| const shim = TOOL_SHIMS[name]; | ||
| if (!shim) return raw; | ||
|
|
||
| let parsed: unknown; | ||
| try { | ||
| parsed = raw && raw.length > 0 ? JSON.parse(raw) : {}; | ||
| } catch { | ||
| parsed = {}; | ||
| } | ||
|
|
||
| const patched = shim(parsed); | ||
| return JSON.stringify(patched); | ||
| } | ||
|
|
||
| // Exposed for unit tests only. | ||
| export const __test = { coerceToArray, TOOL_SHIMS }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation buffers tool arguments for all tool calls, which introduces significant memory overhead for non-shimmed tools. For large tool outputs (e.g., file contents or large data sets), this could lead to excessive memory consumption or OOM errors in high-traffic environments. Buffering should be restricted to only those tools that require shimming.
Additionally, since
argBufferis initialized to""at line 175, the(toolInfo.argBuffer || "")check is redundant.