Skip to content

create_schema produces malformed JSON mid-generation on multi-field schemas (EOF while parsing, 2/3 freq across 3 scenarios) #1836

Closed

Description

@malibio

Overview

Surfaced in #1764's baseline run (commit 57bf1088): create_schema calls with several fields intermittently produce genuinely malformed JSON mid-generation, distinct from the prose/schema-contradiction class of bug this workstream has mostly found (#1809, #1800).

Problem Statement

Scenarios 3, 8a, and 8b — all create_schema calls on entity types with multiple fields — failed at 2/3 frequency each in the baseline run with a JSON parse error:

EOF while parsing a value at line 1 column 192

This is not a prose-vs-schema key contradiction (no core_values/coreValues-style mismatch involved) and not a routing/invocation failure — the tool is called, the model just stops emitting valid JSON partway through the arguments. Column 192 suggests truncation partway into a nested structure (likely a fields array entry or a relationships block), consistent with schemas that have several fields rather than one or two.

Evidence

Suggested Investigation

  • Pull the exact failing generations from the trace file and inspect where truncation occurs relative to field count — check whether there's a field-count or argument-length threshold past which this starts appearing
  • Check MAX_RESPONSE_TOKENS/generation length limits on tool-call argument generation specifically — if this is a token-budget cutoff mid-JSON rather than a model-comprehension failure, the fix is a config/limit change, not a prompt or schema change
  • Rule out interaction with create_schema enum fields silently discarded: prose teaches core_values, serde expects coreValues #1809's deny_unknown_fields change — confirm this predates or is independent of that fix, since a stricter parser rejecting a differently-malformed payload could look similar in the error log but have a different root cause

Architecture Impact

None yet — needs root-causing before it's clear whether this is a generation-length/token-budget issue, a genuine small-model reliability ceiling on deeply nested tool-call arguments (which ADR-038's Evidence section already flags as a known risk for universal/complex argument shapes), or something else.

Activity

  1. malibio commented on Jul 31, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 171 test files passed, 4112 tests passed (45.67s)

  2. self-assigned this
    on Jul 31, 2026
  3. malibio commented on Jul 31, 2026

    @malibio
    CollaboratorAuthor

    Root-cause findings

    Traced the failure mechanism end-to-end. Summary: this is not a token-budget cutoff. All three "Suggested Investigation" checks are resolved below.

    Where the error comes from

    EOF while parsing a value at line 1 column 192 is serde_json's native error text, thrown at agent_loop.rs:1130:

    serde_json::from_str::<serde_json::Value>(&tc.arguments_json)

    tc.arguments_json is assembled in parse_chunks (agent_loop.rs:1726) by straight string-concatenation of every ToolCallArgs chunk the engine emitted for that tool-call id — no truncation logic on the consumer side. So whatever reached this point was already incomplete when it left the engine. Column 192 (~50-60 tokens) is very short — nowhere near any token cap in the system.

    Check 1 — token-budget cutoff: ruled out

    Tool-calling iterations already pass max_tokens: None specifically to avoid this (agent_loop.rs:799, comment: "No cap on tool-calling iterations — truncated args produce invalid JSON"). The GGUF-path fallback in inference.rs:95 defaults None → 2048, but 192 chars is far short of a 2048-token cutoff — this cap isn't what's firing. (Stage-1 routing's 256-token cap, STAGE1_MAX_TOKENS, is a separate turn that never calls create_schema — not implicated either.)

    The actual stop mechanism is chat/mod.rs:561: the token loop breaks immediately and silently whenever model_ref.is_eog_token(new_token) fires — i.e. the model itself emitted an end-of-generation token (EOS/<end_of_turn>) before finishing the JSON. No cap was hit; the model stopped itself mid-argument.

    Check 2 — interaction with deny_unknown_fields (#1816/db6a2289): ruled out

    db6a2289 ("Deny unknown fields on every agent-facing tool param struct") postdates the 57bf1088 baseline this issue's evidence came from (confirmed via git merge-base), so it cannot explain a failure already present in that baseline. Independently of ordering, deny_unknown_fields is a serde struct-field validation error, raised only after the JSON already parses successfully — it cannot produce a raw syntax error like "EOF while parsing a value." Different failure class entirely.

    Check 3 — field-count / argument-length threshold: not corroborated by the committed trace

    The committed trace (scripts/results-e4b.trace.jsonl) only has one sample per scenario, and it's a passing sample: scenarios 3, 8a, 8b all show stage2CandidatesInjected: true, routingDecision: "query", and a successful create_schema call. This is consistent with ADR-038 Evidence Finding 2 — pushing the matched skill's instruction subtree (with its worked example) into the prompt is what makes small models emit correct deeply-nested create_schema calls. Since injection succeeded here and the call still fails 2/3 of the time elsewhere on the same scenarios, the failure looks like run-to-run generation non-determinism, not a deterministic threshold — i.e. exactly the residual small-model reliability gap ADR-038's Evidence section already flags as unresolved ("not sufficient to claim small-model routing is reliable at production rates").

    Likely actual root cause

    Generation is fully unconstrained — chat/mod.rs:1071 passes grammar: None and json_schema: None to llama.cpp's chat-template/sampling setup. There is no grammar-constrained decoding anywhere in nlp-engine; the model free-samples every token (temperature 0.1 + repetition penalty only) and relies entirely on having learned correct JSON structure. LlamaSampler::grammar() is already available in the vendored llama-cpp-2 bindings (used nowhere currently) and would make this class of failure structurally impossible rather than probabilistic — but that's a real feature addition (building/wiring a GBNF or JSON-schema grammar per active tool set), not a one-line fix, and is out of scope for this root-causing pass.

    Suggested next step

    This looks like grammar-constrained decoding is the real fix, but that's a separate, larger issue. Recommend closing this issue as root-caused (small-model non-determinism on complex nested tool-call JSON, absent any output-format constraint) and filing a follow-up for grammar/JSON-schema-constrained sampling on the GGUF chat path — scoped separately since it touches the sampler chain, tool-schema-to-grammar translation, and needs its own eval pass.

  4. malibio commented on Jul 31, 2026

    @malibio
    CollaboratorAuthor

    Next step — how to proceed

    Root-causing above points to grammar-constrained decoding as the real fix: generation is fully unconstrained (chat/mod.rs:1071 passes grammar: None/json_schema: None), so on complex nested create_schema calls the model occasionally emits its end-of-generation token mid-JSON with nothing structurally preventing it. LlamaSampler::grammar() is already available in the vendored llama-cpp-2 bindings but unused anywhere in nlp-engine.

    Three ways to take this:

    1. File a follow-up issue scoped to grammar-constrained decoding, close this one as root-caused. Wiring LlamaSampler::grammar(), translating ToolDefinition.parameters_schema → GBNF/JSON-schema grammar per active tool, and an eval pass to confirm scenarios 3/8a/8b stop failing — real feature work, deserves its own issue and review.
    2. Implement the grammar fix directly in this issue now — same scope as above, just done in this session rather than split out.
    3. Leave the findings as-is for now — no follow-up filed, no code changed, revisit later.

    Proceeding with option 1 (file follow-up + close) unless redirected.

  5. malibio commented on Jul 31, 2026

    @malibio
    CollaboratorAuthor

    Agreed — option 1. This is real feature work (schema→grammar translation, sampler wiring, its own eval pass to confirm 3/8a/8b actually stop failing), and per this workstream's standing pattern that gets its own issue and review rather than riding in on a root-cause issue.

    One ask before closing: when filing the follow-up, carry the specific evidence forward rather than just linking back —

    • The exact code site (chat/mod.rs:1071, grammar: None/json_schema: None) and that LlamaSampler::grammar() is already available but unused in nlp-engine
    • The failing scenarios (3/8a/8b) and the "EOF while parsing a value at line 1 column 192" signature, so whoever picks it up can reproduce before touching code
    • Explicitly note the scope boundary: schema→GBNF/JSON-schema translation per active tool, not a blanket grammar constraint — a follow-up eval pass needs to confirm this doesn't regress tool-calling on schemas that were passing (constrained decoding changing sampling behavior broadly enough to affect passing scenarios would be a bad trade)

    That last point matters given this workstream's track record — a fix that helps the 3 failing scenarios but subtly changes generation on the other 9 is exactly the kind of thing that's slipped through as a clean-looking number before. Make "no regression on scenarios that currently pass" an explicit acceptance criterion on the new issue, not just "3/8a/8b stop failing."

    Proceed with 1.

  6. malibio commented on Jul 31, 2026

    @malibio
    CollaboratorAuthor

    Root-caused. Filed #1838 to carry the fix — grammar-constrained decoding on the GGUF chat path, scoped to per-active-tool JSON-schema→grammar translation, with explicit no-regression-on-passing-scenarios acceptance criteria. Closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions