Skip to content

chat : end Beam analysis on <|constrain|> header variants - #311

Open
danielhanchen wants to merge 1 commit into
masterfrom
fix/harmony-xml-constrain-close
Open

danielhanchen wants to merge 1 commit into
masterfrom
fix/harmony-xml-constrain-close

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

Reflection Beam sometimes closes an empty analysis message with <|constrain|> instead of <|end|> and/or <|start|>. It happens after several earlier assistant turns are sent back without their reasoning, which is what OpenWebUI does by default, for example:

<|channel|>analysis<|message|><|constrain|><|constrain|>assistant<|channel|>final<|message|>Hello ...

The harmony-xml parser only ends an analysis message on <|end|>, <|constrain|><|start|>assistant or <|start|>assistant. It therefore keeps the whole final answer inside reasoning_content, and clients show an empty reply. Upstream report: ggml-org#30735.

Fix

common/parsers/harmony-xml.cpp:

  • also end the analysis body on <|constrain|><|constrain|>assistant<|channel|> and <|constrain|>assistant<|channel|>;
  • after an analysis message, accept <|constrain|>assistant or a bare assistant (only when the next token is <|channel|>) as the next message header, in all four places that previously required <|start|>assistant.

A <|constrain|> at the start of an analysis stays part of the reasoning, and a partial analysis ending in <|constrain|> is still reasoning while streaming.

Tests

tests/test-chat.cpp adds a Reflection Beam block (the model's template is added under models/templates/) with six cases: the normal close, the four <|constrain|> variants, and the streaming partial.

tree test-chat
b11785 + the new tests, parser unchanged fails (Expected: Hello, world!)
this PR All tests passed
nightly merge of all 20 current pins on b11785, plus #298 at d1781c3, plus this PR All tests passed

The patch also applies cleanly on the b11796 merge.

Pinning

This is a pin PR: upstream b11785 (8baabd5) plus this one change. The base branch is base/upstream-8baabd53b. The same commit is suitable for ggml-org; the pin is dropped once upstream takes the fix and the base tag contains it.

Beam sometimes closes an empty analysis message with <|constrain|> instead of
<|end|> and/or <|start|>, which happens after several earlier assistant turns
are sent back without their reasoning. The harmony-xml parser then kept the
whole final answer inside reasoning_content. Treat those header variants as the
end of the analysis message.

Refs ggml-org#30735
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T13:08:44.154259Z 819e95b PR opened
🔒 Security Review ⚠️ Failed 2026-10-21T12:53:09.168992Z
We couldn’t complete this review. Comment @codex security review to try again.
819e95b PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 819e95b316

ℹ️ 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".

Comment on lines +70 to +71
"<|constrain|><|constrain|>assistant<|channel|>",
"<|constrain|>assistant<|channel|>" }));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Recognize malformed closes before tool calls

When tools are enabled and Beam emits either malformed close before a tool call, the next bytes are assistant to=..., not assistant<|channel|>. Because both new delimiters require <|channel|>, analysis_body consumes the tool call through its EOG marker and the optional-tool path returns the wire text as content rather than producing tool_calls. Add equivalent delimiters for the assistant to= form and cover this path with a tool-call test.

Useful? React with 👍 / 👎.

Comment on lines +65 to +68
// The model sometimes closes an empty analysis with <|constrain|> in place of <|end|> and/or <|start|>
// (seen after several earlier turns sent back without reasoning), e.g.
// "<|channel|>analysis<|message|><|constrain|><|constrain|>assistant<|channel|>final<|message|>...".
// Treat those as the end of the analysis message so the reply is not swallowed as reasoning.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Condense the parser-header comments

This four-line comment embeds the full regression example and hard-wraps its explanation, while the adjacent comment also splits a sentence across lines and largely restates next_start. Keep only a concise rationale and leave the concrete variants in the tests, as the repository requires comments to be concise, nonredundant, and not hard-wrapped.

AGENTS.md reference: AGENTS.md:L77-L81

Useful? React with 👍 / 👎.

Comment on lines +69 to +71
auto analysis_body = p.until_one_of(with_eog({ "<|end|>", "<|constrain|><|start|>assistant", "<|start|>assistant",
"<|constrain|><|constrain|>assistant<|channel|>",
"<|constrain|>assistant<|channel|>" }));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Register malformed closes as reasoning end tags

When reasoning_budget_tokens is enabled, these newly accepted sequences end analysis for the PEG parser but not for the reasoning-budget sampler because thinking_end_tags still contains only <|end|>. The sampler therefore keeps counting through the final answer and, if the remaining budget expires, forces <|end|> into the middle of that answer. Publish the compound malformed-close sequences as additional thinking end tags rather than adding bare <|constrain|>, since the latter is explicitly allowed inside reasoning.

Useful? React with 👍 / 👎.

Comment on lines +264 to +266
{%- if "</rfl:parameter>" in value_text -%}
{{- raise_exception("Tool-call parameter '" + (param_name | string) + "' contains the reserved delimiter </rfl:parameter>") }}
{%- endif -%}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject opening parameter delimiters in string values

For an assistant tool-call argument containing a newline followed by <rfl:parameter name="..., this check permits the value even though the Harmony parser treats that opening delimiter as the end of the current string argument and the start of another argument. Re-rendering such a prior call can therefore silently split one value into multiple parameters or make the prompt unparsable; reject or escape the reserved opening delimiter as well as the closing delimiter.

Useful? React with 👍 / 👎.

Comment on lines +175 to +177
{%- elif schema is mapping and schema.get("type") == "array" -%}
{%- if schema.get("items") -%}
{{- json_schema_to_typescript(schema["items"], indent) + "[]" -}}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Parenthesize union array element types

When an array schema has oneOf or enum items, appending [] directly applies the array suffix only to the final union member. For example, string-or-number items render as string | number[], which describes a scalar string or an array of numbers rather than an array containing either type; the model can consequently emit a scalar that the tool grammar rejects. Wrap non-atomic rendered item types in parentheses before adding the suffix.

Useful? React with 👍 / 👎.

Comment on lines +121 to +122
{%- if prop_spec is mapping and "oneOf" not in prop_spec and prop_spec.get("description") is string -%}
{{- (" " * indent) + "// " + prop_spec.description + "\n" -}}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Comment every line of schema descriptions

A multiline property description receives // only on its first line, so subsequent lines are emitted as bare text inside the generated TypeScript tool declaration. Multiline descriptions are valid JSON Schema and this produces a malformed declaration that can change how Beam interprets the tool; split the description and prefix each line as render_tool_declaration already does for function descriptions.

Useful? React with 👍 / 👎.

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