Skip to content

Fix Llama32 tool-call parser truncating text after final call - #28845

Open
codewithfourtix wants to merge 3 commits into
sgl-project:mainfrom
codewithfourtix:fix/llama32-trailing-text-truncation
Open

codewithfourtix wants to merge 3 commits into
sgl-project:mainfrom
codewithfourtix:fix/llama32-trailing-text-truncation

Conversation

@codewithfourtix

@codewithfourtix codewithfourtix commented Jun 21, 2026 •

Copy link
Copy Markdown

Motivation

Llama32Detector.detect_and_parse advanced the cursor by
end + len(self.tool_call_separator) after decoding each JSON tool call,
unconditionally skipping a separator-width character. When free-form text
follows the final call without a separator (e.g. {...}}Some text), this ate
the first character of that text, returning ome text instead of Some text:

'{"name": "get_weather", "parameters": {}}Some follow-up text'
# normal_text -> 'ome follow-up text'   (the 'S' is gone)

The existing test_json_with_trailing_text masked the bug by using a leading
space after the JSON (consumed harmlessly, since the result is .strip()-ed)
plus a substring-only assertIn("follow-up", ...) assertion.

Modifications

- python/sglang/srt/function_call/llama32_detector.py: only consume a
separator when one is actually present after the decoded object, instead of
blindly skipping len(self.tool_call_separator) characters. Multi-call
inputs (;- and <|python_tag|>-separated) are unaffected.
- test/registered/unit/function_call/test_function_call_parser.py: add
test_json_with_immediate_trailing_text, a regression test that fails before
this change and passes after. All existing TestLlama32Detector cases still
pass.

Accuracy Tests

N/A — this only fixes a tool-call text parser on the serving path; it does not
touch kernels or model forward code, so model outputs are unchanged.

Speed Tests and Profiling

N/A — the change replaces an unconditional index increment with an equivalent
bounded check; no measurable impact on inference speed.

Checklist

- [x] Format your code according to the Format code with pre-commit
(https://docs.sglang.io/developer_guide/contribution_guide.html#format-code-with-pre-commit).
- [x] Add unit tests according to the Run and add unit tests
(https://docs.sglang.io/developer_guide/contribution_guide.html#run-and-add-unit-tests).
- [ ] Update documentation according to Write documentations
(https://docs.sglang.io/developer_guide/contribution_guide.html#write-documentations). <!-- N/A: no user-facing
behavior to document -->
- [ ] Provide accuracy and speed benchmark results according to Test the accuracy
(https://docs.sglang.io/developer_guide/contribution_guide.html#test-the-accuracy) and Benchmark the speed
(https://docs.sglang.io/developer_guide/contribution_guide.html#benchmark-the-speed). <!-- N/A: parser-only bug fix,
no accuracy/speed impact -->
- [x] Follow the SGLang code style guidance
(https://docs.sglang.io/developer_guide/contribution_guide.html#code-style-guidance).

Review and Merge Process

1. Ping Merge Oncalls to start the process. See the PR Merge Process
(https://github.com/sgl-project/sglang/blob/main/.github/MAINTAINER.md#pull-request-merge-process).
2. Get approvals from CODEOWNERS (https://github.com/sgl-project/sglang/blob/main/.github/CODEOWNERS) and other
reviewers.
3. Trigger CI tests with comments
(https://docs.sglang.io/developer_guide/contribution_guide.html#how-to-trigger-ci-tests) or contact authorized users
to do so.
  - Common commands include /tag-and-rerun-ci, /tag-run-ci-label, /rerun-failed-ci
4. After green CI and required approvals, ask Merge Oncalls or people with Write permission to merge the PR.

<!-- pr-states:start -->
---
### CI States

Latest PR Test (Base): <!-- slot:pr-test:start -->:x: [Run #33981561119](https://github.com/sgl-project/sglang/actions/runs/33981561119)<!-- slot:pr-test:end -->
Latest PR Test (Extra): <!-- slot:pr-test-extra:start -->:x: [Run #33981560976](https://github.com/sgl-project/sglang/actions/runs/33981560976)<!-- slot:pr-test-extra:end -->
Latest PR Test (AMD ROCm 7.2): <!-- slot:pr-test-amd-rocm720:start -->:x: [Run #33981561275](https://github.com/sgl-project/sglang/actions/runs/33981561275)<!-- slot:pr-test-amd-rocm720:end -->
<!-- pr-states:end -->

`Llama32Detector.detect_and_parse` advanced the cursor by
`end + len(self.tool_call_separator)` after decoding each JSON tool call,
unconditionally skipping a separator-width character. When free-form text
follows the final call without a separator (e.g. `{...}}Some text`), this
ate the first character of that text, returning `ome text` instead of
`Some text`.

Only consume a separator when one is actually present. Add a regression
test; the existing `test_json_with_trailing_text` masked the bug by using
a leading space (stripped anyway) plus a substring-only assertion.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request fixes a bug in the Llama 3.2 function call detector where free-form text immediately following a tool call without a separator would have its first character truncated. It updates the parser to only consume the separator when present and adds a regression test. The reviewer suggests optimizing the separator-skipping logic to avoid string slicing and points out that the same truncation bug still exists in the Python dict fallback path, recommending a fix and an additional test case for it.

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.

This branch has not been deployed

No deployments
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