from typing import Any, Generic, Optional, TypeVar from pydantic import BaseModel, Field # from pydantic.generics import GenericModel T = TypeVar('T') class StandardResponse(BaseModel, Generic[T]): """标准API响应格式""" code: int = Field(200, description="状态码,200表示成功,其他值表示错误") message: Optional[str] = Field(None, description="消息,通常在错误时提供") data: Optional[T] = Field(None, description="响应数据") class Config: from_attributes = True json_schema_extra = { "example": { "code": 200, "message": None, "data": {} } } class ErrorResponse(BaseModel): """错误响应格式""" code: int = Field(500, description="错误码,非200值") message: str = Field(..., description="错误消息") data: None = Field(None, description="数据字段,错误时为null") class Config: from_attributes = True json_schema_extra = { "example": { "code": 500, "message": "业务处理错误", "data": None } }