Decimal fields inside nested models aren't normalized in structured output
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 65/100
Research direction
The recursion lives in normalize_decimal_fields in engine/processing/gsonschema/validators.py, which passes the root schema into nested dict and list values. Run the reproduction snippet against validate() in that file to see the nested price stay a string. Done means nested Decimal fields come back as rounded floats, same-name non-Decimal fields are untouched, and the existing tests in test_validators.py still pass.
Written by the indexing model from the issue text.
Description
Priority Level
Medium (Annoying but has workaround)
Describe the bug
normalize_decimal_fields() (the #171 fix, in engine/processing/gsonschema/validators.py) only handles top-level fields. When it recurses into a nested dict or list it passes the root schema again instead of that field's own schema, so:
- Decimal fields in a nested model, a list of models, or an optional sub-model (
Item | None) keep whatever the LLM returned:"3.25"as a string, or7.257unrounded. - If a batch mixes both forms, the Parquet checkpoint write fails with
ArrowInvalid: Could not convert '3.25' with type str: tried to convert to double, the batch gets dropped, andcreate()ends with "Generated 0 of 4 requested records". Basically #171 again, one level down. - A nested field that shares a name with a top-level Decimal field gets the top-level rule, so a nested
code: strholding"2.25"comes back as the float2.3.
Steps/Code to reproduce bug
import json
from decimal import Decimal
from pydantic import BaseModel, Field
from data_designer.engine.processing.gsonschema.validators import validate
class LineItem(BaseModel):
name: str
price: Decimal = Field(decimal_places=2)
class Order(BaseModel):
total: Decimal = Field(decimal_places=2)
item: LineItem
items: list[LineItem]
schema = Order.model_json_schema()
data = {"total": "10.50", "item": {"name": "pen", "price": "3.25"}, "items": [{"name": "ink", "price": 7.25}]}
out = validate(data, schema, pruning=True, no_extra_properties=True)
print(json.dumps(out))
assert isinstance(out["item"]["price"], float), f"nested price stayed {out['item']['price']!r}"
On main (b802800): AssertionError: nested price stayed '3.25'. The top-level total is normalized to 10.5.
I also have a full preview() + create() repro with the LLM call patched out. That's where the Parquet write fails and 0 of 4 records come out. Happy to paste it.
Expected behavior
Nested Decimal fields get normalized the same way top-level ones do (float, rounded to decimal_places), and nested fields that aren't Decimal are left alone.
Agent Diagnostic / Prior Investigation
I used an agent to trace it. The recursion in normalize_decimal_fields calls normalize_decimal_fields(value, schema) with the root schema for both dict and list values, so nested lookups go against the root properties. A fix that carries the current sub-schema plus the root $defs (following local $ref, list items, and the object branch of anyOf for Model | None) makes the repro pass, and two new tests (nested/optional/list models, and the same-name string case) fail on main and pass with it. The existing 55 tests in test_validators.py still pass.
Searched issues/PRs for decimal, normalize_decimal_fields, nested Decimal: #171 and #182 are about top-level Decimal / the JSON-schema approach in general, #857/#858/#1046 touch nearby code but not this, and no open PR changes gsonschema/validators.py.
Additional context
Python 3.13, pandas 3.0.5, CPU only. I have the fix and tests on a branch and can open a PR once this is triaged.
Checklist
- I reproduced this issue or provided a minimal example
- I searched the docs/issues myself, or had my agent do so
- If I used an agent, I included its diagnostics above
- Dominant language
- Python
- Stars
- 2.4k
- Forks
- 258
- Avg merge
- 4d 20m
- Merged PRs (30d)
- 32
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from NVIDIA-NeMo/DataDesigner
-
Judge score profiler samples reasoning with squared category weights, crowding out uncommon scoresOpen
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
NVIDIA-NeMo/DataDesigner#1202 ·
Maintainers usually reply within 1 day
-
Local seed path containing a single quote breaks the DuckDB queryPossibly taken @xubenqiang claimed this 39 days ago. Openbug triaged
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
NVIDIA-NeMo/DataDesigner#1102 · 3 comments ·
Maintainers usually reply within 1 day
-
enforce \from future import annotations` via ruff FA102 rule`May be free again A pull request for this issue was closed without being merged. Opentask
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
NVIDIA-NeMo/DataDesigner#760 ·
Maintainers usually reply within 1 day
-
NVIDIA-NeMo/DataDesigner#1205 ·
Maintainers usually reply within 1 day
-
NVIDIA-NeMo/DataDesigner#1204 ·
Maintainers usually reply within 1 day
All issues in NVIDIA-NeMo/DataDesigner
Similar issues
-
Test: to_hijri() accepts datetime.datetime and uses only the date partPossibly taken A pull request linked to this issue is open or already merged. Opengood first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 · 3 comments ·
Maintainers usually reply within 1 day