|
1 | | -from pydantic import BaseModel, Field |
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from pydantic import BaseModel, Field, model_validator |
2 | 4 |
|
3 | 5 |
|
4 | 6 | class File(BaseModel): |
5 | | - name: str = Field(..., description="An optional name for the image") |
6 | | - content_type: str = Field(..., description="The content type of the image", examples=["image/png", "image/jpeg"]) |
7 | | - data: str = Field(..., description="The base64 encoded data of the image") |
| 7 | + name: Optional[str] = Field( |
| 8 | + default=None, |
| 9 | + description="An optional name for the file [no longer used]", |
| 10 | + deprecated=True, |
| 11 | + ) |
| 12 | + content_type: Optional[str] = Field( |
| 13 | + default=None, |
| 14 | + description="The content type of the file. Not needed if content type can be inferred from the URL.", |
| 15 | + examples=["image/png", "image/jpeg"], |
| 16 | + ) |
| 17 | + data: Optional[str] = Field( |
| 18 | + default=None, |
| 19 | + description="The base64 encoded data of the file. Required if no URL is provided.", |
| 20 | + ) |
| 21 | + url: Optional[str] = Field( |
| 22 | + default=None, |
| 23 | + description="The URL of the file. Required if no data is provided.", |
| 24 | + ) |
8 | 25 |
|
9 | 26 | def to_url(self) -> str: |
10 | 27 | return f"data:{self.content_type};base64,{self.data}" |
| 28 | + |
| 29 | + @model_validator(mode="after") |
| 30 | + def validate_data_or_url(self): |
| 31 | + if self.url is None and (self.data is None or self.content_type is None): |
| 32 | + raise ValueError("Either data or url must be provided") |
| 33 | + return self |
0 commit comments