🐛 Bug Report: JSONEncoder calling deprecated method `json` for Pydantic models
Which component is this bug for?
Traceloop SDK
📜 Description
We recently turned on deprecation warnings for our app for another purpose, and began getting lots of warnings from Pydantic about Traceloop sdk calling the deprecated json method of Pydantic models. Presumably these models are arguments to workflows and tasks that we have set up.
.../miniconda3/envs/bb3.12/lib/python3.12/site-packages/traceloop/sdk/utils/json_encoder.py:21: PydanticDeprecatedSince20: The `json` method is deprecated; use `model_dump_json` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.11/migration/
return o.json()
👟 Reproduction steps
This contrived example raises the deprecation warning in question:
# make_warning.py
from traceloop.sdk import Traceloop
from traceloop.sdk.decorators import task
from pydantic import BaseModel
Traceloop.init()
class SomeValue(BaseModel):
value: int
@task(name="add_numbers")
def add_numbers(a: SomeValue, b: SomeValue) -> SomeValue:
return SomeValue(value=a.value + b.value)
def main():
result = add_numbers(SomeValue(value=3), SomeValue(value=5))
print(f"Result: {result.value}")
if __name__ == "__main__":
main()
This script assumes the presence of a Traceloop API token in the env.
Call the script with $ python -Wd make_warning.py
👍 Expected behavior
JSONEncoder should call model_dump_json() method of pydantic classes if present.
👎 Actual Behavior with Screenshots
Deprecation warning is raised
🤖 Python Version
3.12.11
📃 Provide any additional context for the Bug.
No response
👀 Have you spent some time to check if this bug has been raised before?
- [x] I checked and didn't find similar issue
Are you willing to submit PR?
Yes I am willing to submit a PR!
Hi, I’d like to work on this issue.
I’ve reviewed the problem, and it appears the code is using model.json() — which is deprecated in Pydantic v2. The recommended approach is to replace it with model.model_dump_json() (or model_dump() depending on usage context).
My plan is:
Identify all instances where .json() is used for Pydantic objects.
Replace those with .model_dump_json() to maintain the same serialization behavior.
Run existing tests and update/add tests if required.
Before I begin implementation, I want to confirm:
Should the update target only deprecated JSON encoding calls, or also cover internal model serialization logic if found?
Is backward compatibility with Pydantic v1 required?
If this direction looks good, I’ll start working on a PR. Thanks! 😄
@Mudavath-kumar a PR is already opened and in progress at https://github.com/traceloop/openllmetry/pull/3517
@ryanamannion are you still working on the PR ? Or Can I take it up ?
this issue is a duplicate one to https://github.com/traceloop/openllmetry/issues/2959 .
I've opened a PR to fix this: #3875
The fix uses model_dump() (Pydantic v2) with a fallback to dict() (Pydantic v1),
avoiding the deprecated .json() method. I used model_dump()/dict() instead of
model_dump_json()/json() because JSONEncoder.default() needs to return Python
objects, not JSON strings.