Repository navigation
fix(serializer): serialize datetime.time and timedelta values (#20158) - #2131
ShayanMuhammad-CS wants to merge 5 commits into
Conversation
chrikrah
left a comment
There was a problem hiding this comment.
@ShayanMuhammad-CS The time and timedelta branches are correct, and all six new tests fail against base and pass here.
non-blocking: #20158 already offered an ISO 8601 duration as the other option, and pydantic picks that one. The same model reads differently in langfuse and in pydantic's own JSON, and a bare 300.0 loses its unit. It's cheap to pick now, before traces store floats.
$ python -m pytest tests/unit/test_serializer.py -q -p no:cacheprovider # Python 3.12.14, langfuse imported from the export
56 passed # head 8d7553f
6 failed, 50 passed # serializer.py from base 077448c
$ python lf_td.py # pydantic 2.14.3, M(at=time(14, 15), timeout=timedelta(minutes=5))
pydantic model_dump_json: {"at":"14:15:00","timeout":"PT5M"}
EventSerializer: {"at": "14:15:00", "timeout": 300.0}
# not run: e2e tests against a Langfuse server
Is there a consumer that needs seconds, or would PT5M work?
|
Thanks for the thorough review and benchmarking, @chrikrah! Switching
I will update the |
…maintainer feedback (#20158)
|
Hi @chrikrah, Updated in commit
Ready for review and merge! |
chrikrah
left a comment
There was a problem hiding this comment.
@ShayanMuhammad-CS, the ISO 8601 switch is the right call, and this can be merged as it stands.
non-blocking: _timedelta_to_iso8601 disagrees with to_jsonable_python from 365 days up, where pydantic counts years (P2Y270DT0.000001S against P1000DT0.000001S). pydantic>=2,<3 is a hard dependency, so the except ImportError branch never runs, so drop it and the helper.
# 9e42e37, based on 077448c
$ python -m pytest tests/unit/test_serializer.py -q -p no:cacheprovider # Python 3.12, langfuse imported from the export
56 passed
# serializer.py from base: 6 failed, 50 passed
$ python probe.py serializer.py # random timedeltas, _timedelta_to_iso8601 from 9e42e37 vs to_jsonable_python
pydantic_core 2.41.4
under 365 days: 0 of 5000 mismatch
>= 365 days: 2000 of 2000 mismatch
P1000DT0.000001S P2Y270DT0.000001S
Is a second ISO implementation worth keeping for a pydantic-less install you can't reach?
…r timedelta (#20158)
|
Hi @chrikrah, Updated in commit
All serializer tests passing cleanly! Ready for merge. |
AI review gate — Waiting for AI review
What to do:
Configuration: the repository's History (latest 5 heads, newest first)
|
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 9e2d1d8. Configure here.
|
/ai-gate check |
Description
Closes langfuse/langfuse#20158
Previously,
EventSerializerinlangfuse._utils.serializerhandleddateanddatetimeobjects, but lacked branches fordatetime.timeanddatetime.timedelta. Because standardtimeandtimedeltainstances lack__slots__and__dict__, they fell through to the unhandled type-name fallback, serializing as"<time>"and"<timedelta>".This also caused Pydantic models containing
timeortimedeltafields to lose their values, asmodel_dump()output recurses throughEventSerializer.default().Fix
timeandtimedeltahandling toEventSerializer._default_inner.datetime.timeviaobj.isoformat().datetime.timedeltavia standard ISO 8601 duration format (e.g."PT5M","PT1M30S","-PT45S"), usingpydantic_core.to_jsonable_pythonwith a zero-dependency fallback_timedelta_to_iso8601, ensuring 100% schema alignment with Pydantic'smodel_dump_json().Testing
Added 6 unit tests to
tests/unit/test_serializer.py:test_time: verifies ISO 8601 formatting ("09:30:00").test_time_with_microseconds_and_timezone: verifies fractional seconds and timezone offset.test_timedelta: verifies ISO 8601 duration formatting ("PT1M30S").test_timedelta_fractional_and_negative: verifies milliseconds ("PT0.25S") and negative intervals ("-PT45S").test_pydantic_model_with_time_and_timedelta: verifies nested Pydantic model serialization ("timeout": "PT5M").test_dict_with_time_and_timedelta: verifies dictionary values ("duration": "PT1H30M").All tests passing:
Note
Low Risk
Localized serializer behavior with added unit coverage; no auth, persistence, or API contract changes beyond richer event JSON.
Overview
Fixes issue #20158 by teaching
EventSerializerto serializedatetime.timeanddatetime.timedeltainstead of emitting opaque"<time>"/"<timedelta>"placeholders.timevalues now useisoformat()(including fractional seconds and timezone).timedeltavalues usepydantic_core.to_jsonable_pythonfor ISO 8601 durations (e.g."PT1M30S","-PT45S"), aligning with Pydantic JSON output when models or dicts nest these types.Six unit tests cover plain values, edge cases, Pydantic models, and dict payloads.
Reviewed by Cursor Bugbot for commit 0a6df35. Bugbot is set up for automated code reviews on this repo. Configure here.