Pydantic fields with a custom serializer get the wrong JSON schema, which crashes the verbose console exporter
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- devtools, observability-sre
Research direction
The schema is built by _pydantic_model_schema in the Logfire JSON formatter path, which reads values via getattr(obj, 'ts') instead of the serialized form. Start by reproducing the datetime example from the issue and check where json_formatter.py's _format_datetime receives the value. The fix should make the schema follow model_dump() output for fields with field, plain or wrap serializers. Done when both examples render as Event(ts='02/01/2020 03:04') and Order(user='Ada') with no exception, and the Pydantic-schema tests pass.
Written by the indexing model from the issue text.
Description
Description
When a Pydantic model field has a custom serializer (@field_serializer, or PlainSerializer / WrapSerializer in Annotated), the logged value is the serialized one, but the JSON schema still describes the field's original type. The verbose console exporter trusts the schema, so a common pattern like formatting a datetime as a string crashes it:
from datetime import datetime
import logfire
from pydantic import BaseModel, field_serializer
logfire.configure(send_to_logfire=False, console=logfire.ConsoleOptions(verbose=True))
class Event(BaseModel):
ts: datetime
@field_serializer('ts')
def ser_ts(self, ts: datetime) -> str:
return ts.strftime('%d/%m/%Y %H:%M')
logfire.info('event {e}', e=Event(ts=datetime(2020, 1, 2, 3, 4)))
File ".../logfire/_internal/json_formatter.py", line 359, in _format_datetime
ValueError: Invalid isoformat string: '02/01/2020 03:04'
The attribute is encoded with model_dump(), so ts is "02/01/2020 03:04", but _pydantic_model_schema builds the schema from getattr(obj, 'ts'), so it says "format": "date-time". The schema sent to Logfire has the same mismatch.
A nested model serialized as a string fails the same way:
class User(BaseModel):
name: str
class Order(BaseModel):
user: User
@field_serializer('user')
def ser_user(self, user: User) -> str:
return user.name
logfire.info('order {o}', o=Order(user=User(name='Ada')))
# AttributeError: 'str' object has no attribute 'items' (json_formatter.py, _format_items)
The same happens with Annotated[..., PlainSerializer(...)], including inside | None and list[...].
Expected: the schema of a field with a custom serializer follows what was actually serialized, so these show Event(ts='02/01/2020 03:04') and Order(user='Ada').
This is the field-level counterpart of #2842, which is about @model_serializer. The fix for that (#2843) doesn't cover field serializers.
Python, Logfire & OS Versions, related packages (not required)
logfire 6.0.0 (main at 137643a5), pydantic 2.14.2, Python 3.12.7, macOS 15.7.3
- Dominant language
- Python
- Stars
- 4.6k
- Forks
- 376
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 124
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from pydantic/logfire
-
numpy.bytes_ attributes are recorded as a truncated repr instead of their contentPossibly taken @JoeyTan21 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Windows: 16 tests in `tests/test_cli.py` fail, and the CI test job only runs on LinuxMay be free again A pull request for this issue was closed without being merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Docs: update the Playground cost estimate note for Gateway custom pricesPossibly taken @henricop claimed this 20 days ago. Open
Difficulty 1/5 Under an hour Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
Custom model_serializer returning a str or list crashes the verbose console exporterPossibly taken @nurlanmnn claimed this 2 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 25/100
Maintainers usually reply within 1 day
All issues in pydantic/logfire
Similar issues
-
A new VOLTTRON_HOME starts with agent isolation mode on (string comparison of a boolean default)Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
llm translation
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
FAIRmat-NFDI/sand#239 ·
Maintainers usually reply within 1 day
-
bug P2
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
coco-research/coco#539 ·
Maintainers usually reply within 1 day
-
dependencies
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
JustBeanie/hass-volcano-hybrid#34 ·
Maintainers usually reply within 1 day