Skip to content

fix(serializer): handle NaN/Inf floats nested in pydantic models - #1811

Open
Shailendra005 wants to merge 1 commit into
langfuse:mainfrom
Shailendra005:fix/serializer-nan-in-pydantic-model
Open

fix(serializer): handle NaN/Inf floats nested in pydantic models#1811
Shailendra005 wants to merge 1 commit into
langfuse:mainfrom
Shailendra005:fix/serializer-nan-in-pydantic-model

Conversation

@Shailendra005

@Shailendra005 Shailendra005 commented Aug 12, 2026

Copy link
Copy Markdown

What does this PR do?

EventSerializer's BaseModel branch returned obj.model_dump() directly, so non-finite floats (NaN/Inf) nested inside a pydantic model skipped the NaN/Inf-to-string conversion that the manual dict/list recursion applies. Python's json C-encoder then emitted bare NaN/Infinity tokens — invalid JSON that the ingestion server's strict parser rejects. This affects the core tracing path (span input/output/metadata serialization).

Fix: recurse the dumped model back through default() (return self.default(obj.model_dump())) so nested non-finite floats get the same safe-string handling as every other path. Behavior for datetimes, enums, UUIDs, dataclasses, media refs, etc. is unchanged (they continue to flow through default()).

Type of change

  • Bug fix

Verification

uv sync --locked
uv run --frozen pytest tests/unit/test_serializer.py tests/unit/test_json.py   # 38 passed
uv run --frozen ruff check langfuse/_utils/serializer.py tests/unit/test_serializer.py
uv run --frozen ruff format --check langfuse/_utils/serializer.py tests/unit/test_serializer.py
uv run --frozen mypy langfuse/_utils/serializer.py --no-error-summary

Added regression test test_pydantic_model_with_non_finite_floats which fails on main (bare NaN emitted) and passes with this change.

Checklist

  • I self-reviewed the diff.
  • I added or updated tests for behavior changes.
  • I did not hand-edit generated files.
  • I did not commit secrets or credentials.

Closes langfuse/langfuse#16048

Greptile Summary

The PR fixes serialization of non-finite floats inside Pydantic models by recursively normalizing model_dump() output before JSON encoding.

  • Converts nested NaN, Infinity, and -Infinity values to safe string tokens.
  • Adds a regression test that validates output with strict JSON parsing.

Confidence Score: 5/5

The PR appears safe to merge, with the changed Pydantic serialization path consistently applying the existing JSON-safe normalization.

The recursive call processes dumped model dictionaries and sequences through established serializer branches, and the regression test verifies that non-finite floats no longer reach the JSON encoder as invalid bare constants.

Reviews (1): Last reviewed commit: "fix(serializer): handle NaN/Inf floats n..." | Re-trigger Greptile

Context used:

The BaseModel branch in EventSerializer returned obj.model_dump() directly, so non-finite floats nested inside a pydantic model bypassed the NaN/Inf-to-string conversion and were emitted as bare NaN/Infinity tokens (invalid JSON). Recurse the dumped model back through default() so nested non-finite floats are converted to safe strings like every other path.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@CLAassistant

CLAassistant commented Aug 13, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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.

[SDK][Python] NaN/Inf floats nested in a pydantic model serialize to invalid JSON (bare NaN/Infinity)

2 participants