hindsight icon indicating copy to clipboard operation
hindsight copied to clipboard

Replies held to a strict schema that never end, and reflect turns that cannot be taken off the grammar

Open Cosmonaut79 opened this issue 2 weeks ago • 1 comments

Seen on a self-hosted Hindsight 1.2.1 (API image by digest) and checked against main at 31d5556 (19 Nov 2026). Paths are under hindsight_api/; line numbers are those of the 1.2.1 image.

Model and provider: Qwen/Qwen3.5-397B-A17B-FP8 behind an OpenAI-compatible API (Infomaniak), with HINDSIGHT_API_LLM_PROVIDER=openai, HINDSIGHT_API_LLM_STRICT_SCHEMA=true and reasoning effort "none". The provider rejects response_format: json_object (HTTP 400), so HINDSIGHT_API_LLM_OPENAI_COMPATIBLE_JSON_MODE=false.

Short

With this model a reply that is held to a strict JSON schema sometimes never ends. The same request, sent again, ends normally. Measured for two schemas by replaying recorded requests with streaming:

Call Strict schema Without the grammar (schema in the prompt)
Retain, fact extraction (1.2.1 fact schema), one recorded request 16 of 64 replies ran into whitespace up to the token limit 32 of 32 ended and were valid
Mental-model refresh, the delta-operations call (DeltaOperationList), 15 recorded requests 27 of 117 replies did not end properly, all of them on 2 of the 15 requests 90 of 90 ended and were accepted
Mental-model refresh, the closing done turn, 15 recorded requests 0 of 104 not tried: it cannot be configured

Retain can be switched (HINDSIGHT_API_LLM_STRICT_SCHEMA_RETAIN=false) and has been stable since. The delta-operations call can be switched (HINDSIGHT_API_LLM_STRICT_SCHEMA_REFLECT=false). The reflect turns cannot.

Hindsight sends these calls without streaming. A reply that never ends is then invisible until a time limit: the client's (300 s here) or the provider's gateway (600 s).

1. Retain: whitespace until the token limit (13 Nov)

1.2.1's fact schema has one more field at the end of each fact than 0.9.2's (from_attachments). One recorded extraction request, replayed with stream: true:

Variant (same messages) Finished Whitespace runaway
Strict, 1.2.1 schema (what production sent) 47 of 64 16 of 64 (one 502)
Strict, 0.9.2 schema 20 of 20 (12 more tries were refused by the rate limit) 0
Strict + frequency_penalty 0.3 28 of 32 4
Strict + repetition_penalty 1.05 29 of 32 3
Strict + guided_whitespace_pattern 26 of 32 6
No grammar, schema appended to the system prompt 32 of 32, all valid 0
  • A runaway emits only whitespace after the facts (26,000 characters in 75 s in one sample) until max_tokens (64,000).
  • It mostly hit short notes with a single fact.
  • In production such a call returned nothing until the client's time limit: one synchronous retain took 611 s (two attempts cut at 300 s, the third answered).
  • On 20 other recorded requests both ways gave 20 of 20 valid replies: 270 facts without the grammar, 232 with it.

2. Mental-model refresh: the delta-operations call (16 Nov)

engine/memory_engine.py 19054 to 19065 calls request_delta_operations with response_format=DeltaOperationList, strict_schema=get_config().llm_strict_schema_reflect, temperature 0 and max_completion_tokens=get_config().reflect_max_completion_tokens, which is unset by default (on purpose, as the comment above the call explains with #3421).

What the strict schema requires. Every property of every operation is required, an optional one is nullable. For AddSectionOp that is op, heading, level, blocks, after_section_id, new_id; the schema that is written into the prompt on the soft path requires only heading and op. new_id comes last.

What was measured. 15 recorded requests of one morning, replayed as production builds them, streamed:

Series Replies Ended properly Ran on Broke off
Request A, strict, no output cap 34 22 9 3
Request A, strict, max_tokens 4,000 12 6 4 2
Request A, strict, max_tokens 2,000 12 6 4 2
Request B, strict 7 4 3 0
The other 13 requests, strict 52 52 0 0
All 15 requests without the grammar 90 90 0 0
  • Ran on: the reply repeats a short unit without end (a code fence with "json", or closing brackets); one wrote only whitespace; request B's repeated whole sentences. Six replies without a cap were still writing when the test cut them after 11 minutes (up to 144,000 characters). With the cap of 2,000 all four ended with finish_reason: length at exactly 2,000 tokens after 47 s. With the cap of 4,000 the test itself cut them after about a minute.
  • Broke off: the stream closed without a finish_reason.
  • Where the reply leaves the schema. The six broken-off replies whose end was recorded all end the same way: after the value of after_section_id come the characters that would close the answer (}]}]}) and a code fence with "json", all of it inside the string. The grammar does not allow the object to end there, because new_id is still due. One reading: the model's next token closes the string and the object in one piece, the grammar forbids that token, and the allowed tokens keep it inside the string.
  • Both requests on which this happened produce an add_section operation in their good replies. Temperature is 0; the same request ends properly in most replies and not in others (24 of 58 for request A).
  • Without the grammar (no response_format, the schema appended to the system prompt as the provider's soft path writes it): 60 replies were checked against the DeltaOperationList model and 30 with parse_delta_operation_list; all were accepted, none was wrapped in a code fence. For request A the model then answered with no operation in 6 of 6 replies; for request B with add_section in 6 of 6, as recorded.

In production with the switch off (HINDSIGHT_API_LLM_STRICT_SCHEMA_REFLECT=false, set the same evening): 14 mental models refreshed one after the other; each of the 14 delta-operations calls ended with a valid operation list at the first ask, in 0.4 to 27.8 s. No stall.

What it does to a refresh. Request A sent without streaming, as Hindsight sends it, 8 times: 5 answers within 10 s; 3 got nothing until the provider's gateway ended the request at 600 s, two with HTTP 504 and one with a normal answer after 602 s. In production one delta-operations call came back as a success after 598.5 s (443 completion tokens; the LLM timeout for refresh calls was 600 s that day), and another was still in flight after 282 s when the task's wall-clock limit ended the refresh. With one permit for refresh calls (HINDSIGHT_API_MENTAL_MODEL_REFRESH_LLM_MAX_CONCURRENT=1) every other refresh waited behind each of them.

3. The reflect turns cannot be taken off the grammar

  • call_with_tools makes every tool turn a structured call with strict_schema=True, written as a literal (engine/llm_wrapper.py 1619 to 1630). HINDSIGHT_API_LLM_STRICT_SCHEMA_REFLECT does not reach it: that flag is read for the delta-operations call (memory_engine.py 19061) and for a reflect's response_schema (engine/reflect/agent.py 399) only.
  • _supports_strict_schema() is true for every provider name but "deepseek" (engine/providers/openai_compatible_llm.py 1975 to 1982). An OpenAI-compatible endpoint with this weakness has no setting that puts the reflect turns on the soft path.
  • The code already knows the failure for tool turns: on OutputTooLongError it samples once more, with the comment "A held reply that loops (whitespace, a digit run) until the cap is a sampling accident" (llm_wrapper.py 1632 to 1637). That works where a cap exists: the loop turns are capped at 1,024 tokens when the closing step writes the answer (_SCHEMA_LOOP_TURN_MAX_TOKENS, agent.py 174).
  • The closing done turn and the delta-operations call have no cap unless HINDSIGHT_API_REFLECT_MAX_COMPLETION_TOKENS is set (agent.py 624, memory_engine.py 19063). That setting is server-wide. A cap large enough for the longest real page here (a done turn of 11,422 tokens) lets a runaway write for more than four minutes at the rate measured (2,000 tokens in 47 s).
  • A delta-operations reply cut at a cap is not sampled again: request_delta_operations asks once more only when a reply parses badly (engine/reflect/delta_ops.py 532 to 558), so OutputTooLongError reaches the except at memory_engine.py 19109 and the refresh fails as delta_ops_failed.

In 104 streamed replays of 15 recorded done turns none ran away. Two such turns in production had the signature of the delta-operations runaways. One came back as a normal answer after 598.3 s (568 completion tokens). The other, the closing turn of a full refresh, got no answer within the client's 300 s limit; the next try answered in 54 s. That is all that is known about them.

4. A side note: the stage named when the wall-clock limit ends a task

_attempt_permits appends .backoff to the stage on any BaseException, a cancellation included (engine/llm_wrapper.py 195 to 206). The worker reads the stage after it has cancelled the executor (worker/poller.py 1335 to 1345). So the message for an attempt that was in flight read stage=llm.openai.mental_model_delta_ops.attempt=1/4.backoff, and on_task_wall_timeout then treated it as cut off during a retry backoff and did not pause the mental model (memory_engine.py 5417 to 5433). The comment in the worker says the stage that was current when the ceiling fired is preserved; with the suffix added on the way out it is not.

What would help

  1. A way to take the grammar off the reflect turns: let call_with_tools follow HINDSIGHT_API_LLM_STRICT_SCHEMA_REFLECT, or a setting that marks a provider as soft-only.
  2. Stream the uncapped structured calls (the done turn, the delta-operations call), or watch them some other way, and end a reply that only repeats a short unit or whitespace; then sample once more, as call_with_tools does at a cap. This needs no output cap, so it does not bring back #3421.
  3. For the strict path, look at the last field of an operation: a trailing nullable field that models usually leave out (new_id) is where these replies left the schema.

Related

  • #6265: a delta-operations reply that runs to the output cap and fails the refresh. Here no cap is set (the default), so the same kind of reply runs until a time limit; and without the grammar it did not occur.
  • #6511 (merged, not released): constrained decoding on vLLM with another model, and a soft path that can send the schema without strict. It does not change the strict path or the tool turns.
  • #6569 asks for the schema on the retraction call. With this model the strict schema is what the runaway replies have in common.
  • On main at 31d5556 call_with_tools still passes strict_schema=True (engine/llm_wrapper.py 1637).

Cosmonaut79 avatar Nov 19 '26 09:11 Cosmonaut79

The table in this report is unusually good, and one row in it is a testable prediction rather than a negative result — worth one more measurement before you decide that schema-permissiveness is the only lever.

guided_whitespace_pattern reduced but did not remove the runaway (6 of 32 vs 16 of 64). Two different mechanisms produce "whitespace until the limit", and they need different guards:

  1. One long run. The grammar masks the token the model wants and whitespace is the only admissible class left, so it emits the same whitespace token hundreds of times. A consecutive-run cap (max_whitespace_cnt, or guided_whitespace_pattern) breaks this.
  2. Interleaved whitespace. The cap resets on every non-whitespace token, so " \n " alternating with "\t", or whitespace punctuated by an occasional , or : that the schema still admits, survives every run-length cap. This is the same ABAB-vs-phrase distinction that makes "count how often this substring repeats" fail on text.

The measurement that separates them is on the 16 failed replies you already recorded: longest whitespace run and whitespace share of total tokens. Run ≫ share means (1); share high with short runs means (2).

If it turns out to be (2), the lever that actually works is an abort, not a grammar patch — and since these calls are non-streaming today, that is a change worth making anyway: stream internally, run a cheap detector on the raw tail (for p in 1..160: tail[-p:] == tail[-2p:-p] catches both the single-token run, the interleaved pattern, and content loops), abort with a repetition reason, retry once. Your own data supports the retry economics: the same request that ran away ended normally on replay, so one bounded retry recovers most of these instead of holding a worker for the 300 s client timeout.

Two smaller things in the report I would not skip:

  • DeltaOperationList: 27 of 117 failing, all on 2 of 15 requests, means the schema interacts with specific content, not with a random seed — a replay of those two requests with the strict schema disabled on the same inputs is the cleanest attribution you can get, and it also tells you whether the fix is "loosen two fields" or "this call should never be schema-bound".
  • The done turn at 0 of 104 with the same grammar machinery is useful evidence that it is not "strict schema is broken" but "specific schema shapes are unreachable for this model" — that reframes the upstream bug report from vLLM/grammar to which schema.

For the detector and the offset-of-loop-start form (harmless to keep in the same helper as the whitespace guard): https://agent-mcocdaa.github.io/agent-output-check/repeat.html — MIT, no dependencies, and I check both implementations against each other on 229 real messages rather than trusting the unit tests.

agent-mcocdaa avatar Nov 19 '26 11:11 agent-mcocdaa

Closing: #6817 is merged. HINDSIGHT_API_LLM_STRICT_SCHEMA_REFLECT=false now also takes reflect's tool turns (the search turns and the closing done) off the strict schema. That was item 1 of "What would help". Retain and the delta-operations call could already be switched off. Since this report, a delta-operations reply that hits the output cap is also sampled once more instead of failing the refresh.

nicoloboschi avatar Nov 24 '26 09:11 nicoloboschi