Skip to content

Fix Root Model Not Rendering As Expected - #1396

Merged
alexmojaki merged 27 commits into
pydantic:mainfrom
dhruv-ahuja:fix/root_model_render
Sep 19, 2025
Merged

alexmojaki merged 27 commits into
pydantic:mainfrom
dhruv-ahuja:fix/root_model_render

Conversation

@dhruv-ahuja

@dhruv-ahuja dhruv-ahuja commented Sep 10, 2025 •

Copy link
Copy Markdown
Contributor

This PR fixes #1107.
Brings over core logic introduced in #1108 PR with additional required changes to make schema non-trivial and ensure apt formatter parsing.

Logs for RootModels encapsulating string literals:

uvp ../rootmodel.py

RootModel[NoneType](root=None)
21:17:00.213 MyRootModel [NoneType]
             │ ../rootmodel.py:48 info
             │ r1=None
             │ r2=None

RootModel[int](root=-9991)
21:17:00.233 MyRootModel [int]
             │ ../rootmodel.py:48 info
             │ r1=-9991
             │ r2=-9991

RootModel[bool](root=False)
21:17:00.235 MyRootModel [bool]
             │ ../rootmodel.py:48 info
             │ r1=False
             │ r2=False

RootModel[float](root=2.0)
21:17:00.236 MyRootModel [float]
             │ ../rootmodel.py:48 info
             │ r1=2.0
             │ r2=2.0

RootModel[str](root='raw_str')
21:17:00.237 MyRootModel [str]
             │ ../rootmodel.py:48 info
             │ r1='raw_str'
             │ r2='raw_str'

@dhruv-ahuja

Copy link
Copy Markdown
Contributor Author

@alexmojaki I was thinking of something like this for the problem discussed in #1108, where r2 schema is trivial, meaning it won't be formatted and data literals in this case will remain in a string form, eg. '2.0' or '"something"'.

If this is the correct approach, how can I test it out locally if I register string data type in JsonArgsValueFormatter class?

image

@alexmojaki

Copy link
Copy Markdown
Contributor
from pydantic import RootModel

import logfire

logfire.configure(console=logfire.ConsoleOptions(verbose=True))


MyRootModel = RootModel[str]

r = MyRootModel('test')
logfire.info('MyRootModel', r1=r, r2=r.root)
#              │ r1='"test"'  # this is bad
#              │ r2='test'

Comment thread logfire/_internal/json_encoder.py Resolved
Comment thread logfire/_internal/json_encoder.py Resolved
@dhruv-ahuja

dhruv-ahuja commented Sep 11, 2025 •

Copy link
Copy Markdown
Contributor Author

@alexmojaki can you please check my changes thus far?
Got the main logic working, however facing an issue with IsJson values being converted to normal dictionaries, like here in CI test.

Please provide pointers on how to deal with this. Meanwhile will try to fix this.
Also please ignore print statements and other testing code, will cleanup at the end.

@alexmojaki

Copy link
Copy Markdown
Contributor

No we only want to change the schema for strings that were inside RootModels since they get an extra layer of JSON encoding that we can't really avoid.

@codecov

codecov Bot commented Sep 12, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Comment thread logfire/_internal/json_schema.py Resolved
@dhruv-ahuja

dhruv-ahuja commented Sep 12, 2025 •

Copy link
Copy Markdown
Contributor Author

@alexmojaki I have made the required changes and updated description with Logfire dashboard screenshots, please check.

edit: added string datatype to json_formatter.py as I assume it was most likely failing on the client side, encountered unsupported datatype error using the ConsoleSpanExporter.

@alexmojaki

Copy link
Copy Markdown
Contributor
from pydantic import RootModel

import logfire

logfire.configure(console=logfire.ConsoleOptions(verbose=True))


MyRootModel = RootModel[str]

r = MyRootModel('test')
logfire.info('MyRootModel', r1=r, r2=r.root)
#              │ r1=test
#              │ r2='test'

I expect

              │ r1='test'
              │ r2='test'

Comment thread logfire/_internal/json_encoder.py Resolved
Comment thread logfire/_internal/json_encoder.py Resolved
Comment thread logfire/_internal/json_formatter.py Resolved
Comment thread logfire/_internal/json_formatter.py Resolved
Comment thread logfire/_internal/json_schema.py Resolved
@dhruv-ahuja

Copy link
Copy Markdown
Contributor Author

@alexmojaki how can I run PR-specific coverage locally? tried the command mentioned here but it runs for all the files
https://github.com/pydantic/logfire/actions/runs/17774267051/job/50518115247?pr=1396

@alexmojaki

Copy link
Copy Markdown
Contributor

I don't know of any way to get coverage without running everything

Comment thread logfire/_internal/json_schema.py Resolved
Comment thread logfire/_internal/json_schema.py Resolved
@alexmojaki

Copy link
Copy Markdown
Contributor

Please add tests for the console exporter, the JSON formatting tests aren't actually very robust.

@dhruv-ahuja

Copy link
Copy Markdown
Contributor Author

@alexmojaki please validate whether the core logic is fine now, meanwhile I'll start looking into the console exporter tests.
Should the tests be the same kind as the json formatter or is there anything else I need to check also?

@alexmojaki

Copy link
Copy Markdown
Contributor

Add tests to test_console_exporter.py in the style of the others there.

Comment thread tests/test_console_exporter.py Resolved
@dhruv-ahuja

dhruv-ahuja commented Sep 18, 2025 •

Copy link
Copy Markdown
Contributor Author

Add tests to test_console_exporter.py in the style of the others there.

Done, added test for StrEnum and IntEnum objects as well to properly cover those scenarios

Comment thread tests/test_json_args_formatting.py Resolved
Comment thread tests/test_json_args.py Resolved

@alexmojaki alexmojaki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patience!

@alexmojaki
alexmojaki enabled auto-merge (squash) September 19, 2025 19:06
@alexmojaki
alexmojaki merged commit 0c3fc81 into pydantic:main Sep 19, 2025
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.

Pydantic RootModel not rendered correctly

2 participants