Skip to content

parser: reject unsafe Gemma 4 tool calls - #12

Draft
pd95 wants to merge 1 commit into
feature/responses-custom-apply-patch-toolfrom
fix/gemma4-tool-call-safety
Draft

pd95 wants to merge 1 commit into
feature/responses-custom-apply-patch-toolfrom
fix/gemma4-tool-call-safety

Conversation

@pd95

@pd95 pd95 commented Jul 27, 2026

Copy link
Copy Markdown
Owner

Motivation

Gemma 4 tool-call parsing includes a best-effort repair path for malformed
model output. A preserved control token inside unfinished arguments could be
mistaken for the end of a call, allowing a truncated prefix to be repaired into
valid JSON and returned as an executable tool call. Other malformed events
could be silently dropped rather than surfaced to the caller.

Model-generated tool arguments are untrusted input. Repair must not turn
control-token injection or incomplete structured output into a command an
agent may execute.

What this changes

  • retains raw tool-call events until the parser can validate the full call;
  • rejects Gemma control tokens and non-whitespace control characters before
    attempting repair;
  • rejects a close token while a quoted string or argument object is incomplete;
  • rejects otherwise unsafe or unrepresentable call shapes;
  • reports an empty tool-call block at end of generation as malformed;
  • preserves valid multiline calls and the existing control-free legacy repair
    behavior.

Dependency and scope

This PR depends on custom apply_patch PR #10 and changes only the Gemma 4
parser and its safety tests. It does not change Gemma model execution or media
support.

Compare:
feature/responses-custom-apply-patch-tool...fix/gemma4-tool-call-safety

Validation

Focused parser tests cover split streaming tokens, raw and quoted truncation,
control characters, empty calls, valid repairs, and chunked multiline content.
The complete model/parsers package, integrated Linux test/lint/build, and
Darwin MLX build gates pass.

Refresh and AI assistance

Reconstructed on Ollama v0.34.0; head
64018404c0fec31e08ecea52ee1ca3f254ea87c3. Codex materially assisted
implementation review, tests, refresh validation, and this PR description.

@pd95
pd95 force-pushed the fix/gemma4-tool-call-safety branch from 3836daa to be842b2 Compare August 8, 2026 17:37
@pd95 pd95 changed the title fix(parser): reject unsafe Gemma4 tool calls parser: reject unsafe Gemma 4 tool calls Aug 8, 2026
@pd95
pd95 changed the base branch from main to feature/responses-custom-apply-patch-tool August 8, 2026 17:41
@pd95
pd95 force-pushed the fix/gemma4-tool-call-safety branch 2 times, most recently from c7a2752 to f767266 Compare August 14, 2026 21:08
@pd95
pd95 force-pushed the fix/gemma4-tool-call-safety branch from f767266 to 7e48aba Compare August 23, 2026 07:27
@pd95
pd95 force-pushed the fix/gemma4-tool-call-safety branch 2 times, most recently from 851fdd9 to 9979c21 Compare August 31, 2026 05:38
@pd95
pd95 force-pushed the fix/gemma4-tool-call-safety branch from 9979c21 to ba4e7ff Compare September 5, 2026 09:33
@pd95
pd95 force-pushed the fix/gemma4-tool-call-safety branch from ba4e7ff to 6401840 Compare September 14, 2026 06:09
@pd95
pd95 removed this pull request from stack #26 September 14, 2026 18:24
@pd95
pd95 added this pull request to stack #27 September 14, 2026 18:24
Co-authored-by: Codex <noreply@openai.com>
@pd95
pd95 force-pushed the fix/gemma4-tool-call-safety branch from 6401840 to e9d3eee Compare September 18, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant