Skip to content

Commit 3130064

Browse files
Vamsi-kluclaude
andcommitted
Fix structlog JSON serialization crash 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: #62472 Closes: #62201 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 94ece75 commit 3130064

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

shared/logging/src/airflow_shared/logging/structlog.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,17 @@ def json_dumps(msg, default):
316316
"event": msg.pop("event"),
317317
**msg,
318318
}
319-
return msgspec.json.encode(msg, enc_hook=default)
319+
320+
def safe_default(obj):
321+
"""Fall back to str() if the default enc_hook fails or is None."""
322+
if default is not None:
323+
try:
324+
return default(obj)
325+
except TypeError:
326+
pass
327+
return str(obj)
328+
329+
return msgspec.json.encode(msg, enc_hook=safe_default)
320330

321331
json = structlog.processors.JSONRenderer(serializer=json_dumps)
322332

0 commit comments

Comments
 (0)