Pydantic fields with a custom serializer get the wrong JSON schema, which crashes the verbose console exporter

Open
#2,856 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@MaiAditya is already working on this.

Since Nov 21, 2026.

  • #2857 by @MaiAditya — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
fastapi, python

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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pydantic/logfire

All issues in pydantic/logfire

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.