Skip to content

Fix structlog JSON serialization crash on non-serializable objects#62656

Open
Vamsi-klu wants to merge 1 commit intoapache:mainfrom
Vamsi-klu:fix/m8-structlog-json-serialization-fallback
Open

Fix structlog JSON serialization crash on non-serializable objects#62656
Vamsi-klu wants to merge 1 commit intoapache:mainfrom
Vamsi-klu:fix/m8-structlog-json-serialization-fallback

Conversation

@Vamsi-klu
Copy link
Contributor

@Vamsi-klu Vamsi-klu commented Mar 1, 2026

Summary

  • Wrap enc_hook in json_dumps() with a safe_default() fallback that catches TypeError and falls back to str()
  • Prevents the logging pipeline from crashing when log event dicts contain objects that msgspec cannot serialize
  • Defensive fallback only engages when normal serialization paths fail

Co-contributors : @codingrealitylabs @girlcoder-gaming

Test plan

  • Verify logging with standard serializable objects still works
  • Verify logging with non-serializable objects (e.g., custom classes) falls back to str() instead of crashing
  • Run pytest task-sdk/tests/ -k log -v

Closes: #62472
Closes: #62201

🤖 Generated with Claude Code

@Vamsi-klu Vamsi-klu marked this pull request as ready for review March 1, 2026 07:35
@Vamsi-klu
Copy link
Contributor Author

cc @ashb @amoghrajesh @potiuk — Would appreciate your review. This adds a str() fallback in the structlog JSON serializer to prevent logging pipeline crashes on non-serializable objects.

Wrap the enc_hook in json_dumps() with a safe_default() fallback that
catches TypeError and falls back to str(). This prevents the logging
pipeline from crashing when log event dicts contain objects that
msgspec cannot serialize.

Closes: apache#62472
Closes: apache#62201

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@Vamsi-klu Vamsi-klu force-pushed the fix/m8-structlog-json-serialization-fallback branch from 087f1a6 to 3130064 Compare March 1, 2026 17:44
@Vamsi-klu Vamsi-klu requested a review from potiuk as a code owner March 1, 2026 17:44
@potiuk
Copy link
Member

potiuk commented Mar 2, 2026

You need to (as always) add test to that change - including covering the case that crashed before from the linked issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logging dictionary object in task fails (structlog/stdlib misconfiguration) structlog configuration fails with dict input

2 participants