You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(llmobs): improve serialization defaults for pydantic models [backport 3.2] (#12767)
Backport cf86284 from #12635 to 3.2.
[MLOB-2314]
Cherry-pick from @helmus' fix in #12617, only additions are tests and
release note. Thanks @helmus for the contribution!
This PR improves our serialization defaults for non-JSON serializable
objects, primarily pydantic models and enum/sentinel classes.
- pydantic models are not JSON serializable directly, but have a
`model_dump()` method which converts the object into a json serializable
representation. We'll account for this in our serialization logic,
although there is a small risk that a non-pydantic object also
implements `model_dump()` without being json-serializable. However this
should still be caught in our non-serializable default code.
- enum/sentinel classes (like OpenAI's `NOT_GIVEN`
[class](https://github.com/openai/openai-python/blob/main/src/openai/_types.py#L133))
are not JSON serializable but usually have a str/repr implementation.
We'll try converting the object to a string first before defaulting to
our placeholder (`[Unserializable object: <repr(obj)>]`).
Note: this PR does not introduce a library dependency, only adds a
testing dependency on `Pydantic`.
## Checklist
- [x] PR author has checked that all the criteria below are met
- The PR description includes an overview of the change
- The PR description articulates the motivation for the change
- The change includes tests OR the PR description describes a testing
strategy
- The PR description notes risks associated with the change, if any
- Newly-added code is easy to change
- The change follows the [library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
- The change includes or references documentation updates if necessary
- Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
## Reviewer Checklist
- [x] Reviewer has checked that all the criteria below are met
- Title is accurate
- All changes are related to the pull request's stated goal
- Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- Testing strategy adequately addresses listed risks
- Newly-added code is easy to change
- Release note makes sense to a user of the library
- If necessary, author has acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment
- Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
[MLOB-2314]:
https://datadoghq.atlassian.net/browse/MLOB-2314?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
0 commit comments