0z5a opened a new pull request, #1042:
URL: https://github.com/apache/burr/pull/1042
A pydantic model in state cannot be saved by any of the JSON-based
persisters if it has a field such as a `datetime`, `UUID` or `Decimal`. The
pydantic serde plugin calls `model_dump()`, which keeps those values as Python
objects, so the persister's `json.dumps(state.serialize())` raises `TypeError`
and the step fails.
## Changes
- `burr/integrations/serde/pydantic.py`: `serialize_pydantic` now calls
`model_dump(mode="json")`. Pydantic then emits JSON-compatible values (ISO
strings for datetimes, strings for UUIDs and Decimals, and so on), and
`model_validate` turns them back into the original types on load.
- `tests/integrations/serde/test_pydantic.py`: new test that round-trips a
model with `UUID`, `datetime` and `Decimal` fields through `State.serialize()`,
`json.dumps`/`json.loads` and `State.deserialize()`.
## How I tested this
Minimal repro on main:
```python
import datetime, decimal, os, tempfile, uuid
import pydantic
from burr.core import ApplicationBuilder, State, action
from burr.core.persistence import SQLitePersister
class Order(pydantic.BaseModel):
order_id: uuid.UUID
placed_at: datetime.datetime
total: decimal.Decimal
@action(reads=[], writes=["order"])
def make(state: State) -> State:
return state.update(order=Order(order_id=uuid.uuid4(),
placed_at=datetime.datetime(2026, 1, 2), total=decimal.Decimal("9.99")))
with tempfile.TemporaryDirectory() as d:
persister = SQLitePersister(db_path=os.path.join(d, "s.db"))
persister.initialize()
app = (
ApplicationBuilder()
.with_actions(make)
.with_transitions()
.with_entrypoint("make")
.with_state_persister(persister)
.with_identifiers(app_id="app")
.build()
)
app.step()
```
On main, `app.step()` raises `TypeError: Object of type UUID is not JSON
serializable` from the persister. A model with only `str`/`int` fields saves
and loads fine, so the problem is limited to field types that aren't native
JSON. With this change the step succeeds, and `persister.load("",
"app")["state"]["order"]` is equal to the original model.
The new test fails on main with the same `TypeError` and passes with the
fix. `tests/core`, `tests/tracking`, `tests/integrations/serde`,
`tests/integrations/test_burr_pydantic.py` and
`tests/integrations/persisters/test_b_aiosqlite.py` pass, except the graphviz
tests in `tests/core/test_graphviz_display.py`, which fail the same way on main
because the `dot` binary isn't installed on my machine. black, isort and flake8
(pre-commit settings) are clean on the changed files.
## Notes
- `State.serialize()` is documented as returning a JSON-serializable object,
and the JSON persisters (SQLite, aiosqlite, psycopg2, asyncpg, Redis, MongoDB)
all store `json.dumps(state.serialize())`. Pydantic's `mode="json"` is its
documented way to get only JSON-compatible types from `model_dump`. I
reproduced this with the SQLite persister only.
- The local tracking client already writes state through pydantic's
`model_dump_json()`, so its log lines are byte-identical before and after this
change. This only changes the path that was failing.
- One behavior change: if a model has a field of a type pydantic can't
serialize to JSON (with `arbitrary_types_allowed`), `State.serialize()` now
raises a `ValueError` that names the field. Before, it returned a dict that the
persister's `json.dumps` then rejected. Every built-in persister and tracker
stores JSON, so those models already failed to save. The difference only shows
up in a custom persister that stores the serialized dict without JSON-encoding
it. Infinite and NaN floats are unchanged: they are written and read back the
same way as before.
- Models with `strict=True` and such fields can now be saved, but loading
still fails, because strict validation in Python mode rejects an ISO string for
a `datetime`. Loading these models from tracker logs fails the same way on main
today, so I left that out of this change.
## Checklist
- [x] PR has an informative and human-readable title (this will be pulled
into the release notes)
- [x] Changes are limited to a single goal (no scope creep)
- [x] Code passed the pre-commit check & code is left cleaner/nicer than
when first encountered.
- [x] Any _change_ in functionality is tested
- [ ] New functions are documented (with a description, list of inputs, and
expected output)
- [ ] Placeholder code is flagged / future TODOs are captured in comments
- [ ] Project documentation has been updated if adding/changing
functionality.
This change was drafted with Claude Code (claude-opus-5-5); the test results
above are from the runs described.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]