Serialize pydantic-ai message.
The ctx field in a RetryFeedbackPart's error details is optionally dict[str, Any],
which is not always serializable.
Source code in src/llmling_models/utils.py
| def serialize_message(message: PydanticAIMessage) -> str:
"""Serialize pydantic-ai message.
The `ctx` field in a `RetryFeedbackPart`'s error details is optionally dict[str, Any],
which is not always serializable.
"""
for part in message.parts:
if isinstance(part, RetryFeedbackPart) and isinstance(part.content, list):
for content in part.content:
content["ctx"] = {k: str(v) for k, v in (content.get("ctx", None) or {}).items()}
return message_adapter.dump_python(message, mode="json") # type: ignore[no-any-return]
|