Skip to content

fix(core): serialize nested BaseModel instances in to_json_attr - #143

Closed
kcncb wants to merge 1 commit into
genkit-ai:mainfrom
kcncb:fix/to-json-attr-nested-models
Closed

kcncb wants to merge 1 commit into
genkit-ai:mainfrom
kcncb:fix/to-json-attr-nested-models

Conversation

@kcncb

@kcncb kcncb commented Nov 18, 2026

Copy link
Copy Markdown

Fixes #80.

Problem

to_json_attr only checked isinstance(value, BaseModel) at the top level, so anything that held models — list[Reservation], dict[str, Reservation] — went through plain json.dumps(value), raised TypeError, and fell back to str(value):

to_json_attr({'bookings': [Reservation(party_size=2)]})
# => "{'bookings': [Reservation(party_size=2, notes=None)]}"   # not JSON

Input/output span attributes that are documented to be JSON were then unparseable by any consumer.

Change

json.dumps now takes a default handler that encodes the leftover leaf with pydantic_core.to_jsonable_python(..., by_alias=True, exclude_none=True, bytes_mode='base64', fallback=str) — the same encoding genkit-otel uses in _encode_json_value (from #39). Nested models, datetimes, UUIDs, bytes and dataclasses all land as JSON scalars instead of a repr.

The top-level BaseModel fast path is untouched (model_dump_json, alias + exclude_none), and str(value) is still the last resort.

Tests

New packages/genkit/tests/genkit/core/to_json_attr_test.py, five cases:

case expected
{'bookings': [Reservation(partySize=2)]} {"bookings": [{"partySize": 2}]} (alias kept, notes=None dropped)
[Reservation(partySize=3, notes='window')] valid JSON list
bare model unchanged {"partySize":4}
{'at': datetime(...)} ISO-8601 scalar
{'weird': object()} still a string, never raises
$ uv run --frozen pytest packages/genkit/tests/genkit/core/to_json_attr_test.py -q -o addopts=""
5 passed

# source reverted, tests kept
3 failed, 2 passed      # the nested / list / datetime cases

$ uv run --frozen pytest to_json_attr_test.py run_in_new_span_test.py instrumentation_test.py telemetry_test.py -q
77 passed

$ uv run --frozen ruff check … && ruff format --check …
All checks passed! / 2 files already formatted

@google-cla

google-cla Bot commented Nov 18, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@kcncb
kcncb force-pushed the fix/to-json-attr-nested-models branch from fad3e84 to f285566 Compare November 18, 2026 06:43
@kcncb

kcncb commented Nov 18, 2026

Copy link
Copy Markdown
Author

CLA is green now: cla/google passes on the current head f285566. The first push carried my local git identity, so the bot could not match the commit author to the already-signed account; the same commit re-authored to my nopeply GitHub address is what you see here. No re-signing is needed — the older CLA notice above refers to the superseded commit.

@huangjeff5

Copy link
Copy Markdown
Collaborator

Thanks for your interest in Genkit. We're closing this pull request because we can't accept it in its current form. If you'd like to contribute, please start a discussion with the maintainers on an issue first; see CONTRIBUTING.md.

@huangjeff5 huangjeff5 closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): serialize nested BaseModel instances in to_json_attr

2 participants