refactor(guardrails): generalize _to_dict to any pydantic model

Addresses PR review: _to_dict special-cased LitellmParams and returned an
empty dict for any other pydantic model. Switch to isinstance(value,
BaseModel) so it coerces any pydantic model uniformly (BaseModel is already
imported for the response models), which also drops the now-unused
LitellmParams import. Behavior is unchanged for the current call sites.
This commit is contained in:
Yuneng Jiang 2026-07-10 17:24:36 -07:00
parent 799a559871
commit 8b31b200ca
No known key found for this signature in database

View file

@ -20,7 +20,6 @@ from litellm.repositories.table_repositories import (
SpendLogGuardrailIndexRepository,
SpendLogsRepository,
)
from litellm.types.guardrails import LitellmParams
router = APIRouter()
@ -146,8 +145,8 @@ def _get_guardrail_field(g: Any, field: str) -> Any:
def _to_dict(value: Any) -> Dict[str, Any]:
"""Coerce a LitellmParams / guardrail_info value into a plain dict."""
if isinstance(value, LitellmParams):
"""Coerce a pydantic model (e.g. LitellmParams) / dict value into a plain dict."""
if isinstance(value, BaseModel):
return value.model_dump(exclude_none=True)
if isinstance(value, dict):
return value