baml icon indicating copy to clipboard operation
baml copied to clipboard

0.17.0 parser: an optional class field is parsed as null when ≥2 of its optional fields are absent (present object with all required fields)

Open BenSpex opened this issue 3 months ago • 3 comments

Summary

On BAML 0.17.0 (CLI baml run, Rust SDK likely identical parser), an optional class-typed field is parsed as null whenever the class has two or more optional fields absent from the model's JSON — even though the object is present and every required field parses. With one absent optional field the class parses; with the absent fields spelled as explicit null it parses.

Minimal repro

class Inner { x: float, y: float }
class Mid   { k: string, a: int?, b: int? }
class Outer { name: string, mid: Mid? }

function Echo(raw: string) -> Outer[] {
    client: <any OpenAI-compatible client>
    prompt: `${role("user")}Return exactly this JSON, unchanged (do NOT add missing keys):
${raw}
${ctx.output_format}`
}

Input raw = [{"name": "a", "mid": {"k": "v"}}, {"name": "b", "mid": {"k": "w"}}]

Model output (captured through a logging proxy, verbatim): [{"name": "a", "mid": {"k": "v"}}, {"name": "b", "mid": {"k": "w"}}]

baml run --output-format json prints: [{"name":"a","mid":null},{"name":"b","mid":null}]

Variations run on the same call path (raw output verified through the proxy each time):

schema of Mid absent keys in JSON parsed mid
{ k: string, a: int?, b: int? } a, b null
{ k: string, a: int?, b: int?, inner: Inner? } (inner present) a, b null
{ k: string, a: int?, b: int?, inner: Inner } (inner present) a, b null
{ k: string, opt: int?, inner: Inner? } (inner present) opt ok
{ k: string, a: int?, b: int? } with "a": null, "b": null written none ok
{ k: string, inner: Inner? } none ok

So the trigger is exactly "≥2 optional fields absent inside an optional class"; the same shape as a NON-optional field (mid: Mid) parses fine. It looks like the schema-aligned parser's per-missing-field penalty pushes the class candidate's score below the null candidate's once two optionals are missing.

Real-world impact

A provenance envelope of the form class Wrapped { value: T?, source: Source?, flag: bool, note: string? } with class Source { id: string, page: int, bbox: Bbox?, start: int?, end: int?, confidence: float, kind: Kind }: a vision extractor answers with a bbox and (naturally) no start/end → every source comes back null although the raw model output carries the boxes (one page: 9 real boxes in the raw response, 42/42 source: null in the parsed output). Every grounding of every vision function is silently lost at the seam; nothing in the CLI output or logs flags it.

Workaround we ship: the prompt example spells "start": null, "end": null so the model writes them explicitly.

Environment

  • baml wrapper 0.2.0, BAML_VERSION=0.17.0 (baml toolchain has no newer version to try: 0.17.1 / 0.18.0 → 404 on the manifest)
  • Model: a local OpenAI-compatible server — the raw JSON was captured by a reverse proxy, so this is the parser, not the model.
  • Linux x86_64.

BenSpex avatar Aug 27 '26 16:08 BenSpex

BAMLGH-68

linear[bot] avatar Aug 27 '26 16:08 linear[bot]

I spent some time trying to reproduce this against current main and couldn't — sharing the matrix in case it helps narrow down the environment delta:

Shape tried (jsonish::from_str, non-streaming, allow_checks=true) Result
Issue's exact repro: Outer[] with mid: Mid? ({k, a?, b?}), 2 absent optionals ✅ mid parses as {"k":"v","a":null,"b":null}
1 absent / 3 absent optionals inside the optional class ✅ parses
Optional class value directly (no list/outer wrapper) ✅ parses
Text-wrapped JSON (Here is the JSON: [...] → AnyOf path) ✅ parses
Markdown-fenced JSON ✅ parses
<think>...</think> prefix before the JSON (Qwen-style reasoning wrapper) ✅ parses
The real-world Grounded/SourceSpan shape from this report (3 absent optionals in the nested class, outer optional fields too) ✅ parses

Pipelines: baml run (CLI), the async interpreter, and the VM runtime all funnel into jsonish::from_str (PromptRenderer::parse), and coerce_union.rs on the current baml-language-0.18.1-nightly.* builds is byte-identical to main — the jsonish coercer hasn't changed since December, so 0.17.0 and main should agree here.

A few things that might explain the delta:

  1. baml wrapper 0.2.0 + BAML_VERSION=0.17.0 with 0.17.1/0.18.0 → 404 on the manifest suggests the local install may not be a stock build — could you share the output of baml version and, if possible, re-test on a current nightly?
  2. Since the model is Qwen 3.6 via vLLM, could the proxy capture and the completion that actually reaches the parser differ (e.g. reasoning-tag stripping)? A captured baml.log (BAML_LOG=jsonish=trace) for one failing call would pin down exactly which candidate values the coercer sees.
  3. The full baml_src of the failing function (the real Grounded schema has an enum + nested optional class — a subtly different shape could still trigger it).

If it turns out the parser is at fault after all, the score-comparison path in coerce_union/pick_best (per-missing-field OptionalDefaultFromNoValue penalties vs. the null candidate) is the right place to look — but I can't make it lose to null on current code.

Huang-404-Q avatar Sep 07 '26 09:09 Huang-404-Q

Rechecked on 2026-09-08 using the official Linux x86_64 GNU release binaries. The minimal repro already in this public issue fails on 0.17.0 and passes on both 0.18.0 and 0.18.1-nightly.20260906.a.

Public repro: optional Mid with a and b omitted Result
0.17.0 Both present mid objects become null
0.18.0 Both objects retained; missing members default to null
0.18.1-nightly.20260906.a Both objects retained; missing members default to null

This comparison used an identical fixed HTTP completion, eliminating model-output variation. Each invocation exited 0 and made one HTTP request. The 0.17.0 binary matched a fresh download of the official release byte for byte.

The version command is baml --version. For logging, 0.18 rejects BAML_LOG=jsonish=trace; BAML_LOG=trace is accepted.

From the reporting side, the minimal defect is confirmed fixed in the tested 0.18 builds. This result is scoped to #4610 and does not establish the status of other issues.

Tested releases: 0.18.0, 0.18.1-nightly.20260906.a.

BenSpex avatar Sep 08 '26 09:09 BenSpex