fix(retain): constrain occurred_start/occurred_end so grammar-constrained models can't emit prose - #3845
Merged
nicoloboschi merged 4 commits intoAug 31, 2026
Conversation
Strix Security ReviewWarning This pull request has 30 commits after the last Strix review ( No security issues found. Updated for Reviewed by Strix |
nickanderson
marked this pull request as draft
August 27, 2026 17:58
nickanderson
marked this pull request as ready for review
August 27, 2026 22:46
…p pattern Both fields are LLM-facing but typed as bare `str | None`, so the generated JSON schema advertises "any string". Under grammar-constrained decoding (response_format: json_schema -> GBNF) a description is not a constraint, and the model can put prose in the field. When asked for a timestamp it cannot derive -- e.g. a narrative duration with no start time -- it reasons inside the string value and runs to the completion cap, producing a corrupted record plus a truncated body with finish_reason: "length" that then gets retried byte-identical (vectorize-io#3811, vectorize-io#3683). Constrain the string branch with a pattern permissive about precision (date only, date+time, optional seconds/fraction, optional Z or offset) and strict about everything else. null stays valid as the escape hatch for "no derivable date"; _infer_temporal_date() already backfills from the text. The internal Fact model is deliberately left lenient: it is the post-parse storage model, not an LLM contract. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Assert the LLM-facing schemas expose a pattern-constrained string branch and keep null, that real timestamp formats still validate, that prose (including the observed runaway string) is rejected, and that the internal Fact model stays lenient. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…ity flag The `pattern` keyword is only usable on backends that accept it, and rejection is a hard 400 at request time rather than a degraded response: Bedrock validates structured-output schemas against an allowlist that excludes it, and OpenAI errors on unsupported keywords under `strict`. Backends that neither enforce nor reject it (native Anthropic tool schemas) gain nothing from it either. So the constraint is no longer declared on the fact models. It is layered on at schema-build time by `_with_iso_timestamp_pattern()` when HINDSIGHT_API_LLM_SUPPORTS_STRING_PATTERN is set, mirroring the existing `llm_supports_max_items` escape hatch that exists for the same reason. Constraining the Pydantic model rather than post-processing the serialized schema is what makes it uniform across providers -- Gemini is handed the response model itself, not a schema dict. Tests now pin both halves: no `pattern` in the schema by default across every extraction mode, and when enabled, a constrained string branch that stays nullable, composes with the entity-labels model, accepts the timestamp formats real models emit, and rejects prose.
nicoloboschi
force-pushed
the
fix/occurred-timestamp-pattern
branch
from
August 31, 2026 10:03
6772cb3 to
9685f27
Compare
Two CI follow-ups to the capability flag: - skills/hindsight-docs is generated from hindsight-docs; the new configuration.md row has to be regenerated into it or verify-generated-files fails on the drift. - test_retain_cacheable_prefix_invariant_to_per_bank_freetext builds its config as a SimpleNamespace, which raises AttributeError on a field it doesn't declare. Adding the field to the stub keeps the failure mode honest -- production passes a real HindsightConfig, so a stub that silently defaulted via getattr would hide a genuine config-shape error.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
occurred_startandoccurred_endare LLM-facing fields typed as barestr | None, so the generated JSON schema advertises "any string". Theirdescriptions say "ISO timestamp", but under grammar-constrained decoding
(
response_format: json_schema→ GBNF) a description is not a constraint —the grammar is the only thing standing between the model and an arbitrary
string.
When the model is asked for a timestamp it cannot derive — e.g. a narrative
duration with no start time — it reasons inside the string value:
Two failures from one root cause:
occurred_startis a paragraph ofthe model's reasoning rather than a date.
completion budget (2500/2500, and 4,598+ tokens on a larger cap) producing an
unterminated JSON body with
finish_reason: "length", which the retain paththen re-sends byte-identical. That is the failure class described in finish_reason: "length" on the non-streaming .create() path is treated as a parse error, not OutputTooLongError — the fact-extraction auto-split (#2579/#3174) is still dead code for the truncation subclass #3811
and openai_compatible_llm: bare json.loads + identical retries — parse_llm_json/json_repair never reached, deterministic failures burn all 4 attempts and drop the chunk #3683; this PR removes one of its triggers.
Reproduced with
gemma-4-26B-A4B-it(Q4, Q6 and Q8) w/ llama.cpp.Field emission order is significant here and is not always the client's to control. llama.cpp compiles
propertiesinto the grammar in order and enforces it.re-serializes the request body with a sorted-map JSON type silently alphabetizes
properties— which putsoccurred_end/occurred_startahead ofwhat— and the client cannot detect this, because the schema is still==equal as an object. (I hit this with one such proxy.
this PR fixes the symptom.)