feat: add input data to pydantic validation error logging (#5748)

* idk letta coded lemme review in gh

* remove unused imports
This commit is contained in:
Kian Jones
2025-10-29 18:03:07 -07:00
committed by Caren Thomas
parent f65eaa10c1
commit 0036976448
2 changed files with 19 additions and 4 deletions

View File

@@ -1,10 +1,10 @@
import uuid
from datetime import datetime
from logging import getLogger
from typing import Optional
from typing import Any, Optional
from uuid import UUID
from pydantic import BaseModel, ConfigDict, Field, field_validator
from pydantic import BaseModel, ConfigDict, Field, ValidationError, field_validator, model_validator
# from: https://gist.github.com/norton120/22242eadb80bf2cf1dd54a961b151c61
@@ -12,7 +12,21 @@ from pydantic import BaseModel, ConfigDict, Field, field_validator
logger = getLogger(__name__)
class LettaBase(BaseModel):
class LoggingBaseModel(BaseModel):
"""Base model with global validation error logging for all pydantic models."""
@model_validator(mode="wrap")
@classmethod
def _log_validation_errors(cls, values: Any, handler, info):
"""Global validator to log validation errors with the full data that failed validation."""
try:
return handler(values)
except ValidationError as e:
logger.error(f"Pydantic validation error in {cls.__name__}. Input data: {values}. Error: {e}")
raise
class LettaBase(LoggingBaseModel):
"""Base schema for Letta schemas (does not include model provider schemas, e.g. OpenAI)"""
model_config = ConfigDict(

View File

@@ -5,6 +5,7 @@ from typing import Annotated, List, Literal, Optional, Union
from pydantic import BaseModel, Field, field_serializer, field_validator
from letta.schemas.letta_base import LoggingBaseModel
from letta.schemas.letta_message_content import (
LettaAssistantMessageContentUnion,
LettaUserMessageContentUnion,
@@ -22,7 +23,7 @@ class MessageReturnType(str, Enum):
tool = "tool"
class MessageReturn(BaseModel):
class MessageReturn(LoggingBaseModel):
type: MessageReturnType = Field(..., description="The message type to be created.")