Skip to content

Commit ad3794b

Browse files
authored
Merge pull request #16 from workflowai/guillaume/fix-file-type
Add url to file type
2 parents 5ebd5a2 + 57861b8 commit ad3794b

3 files changed

Lines changed: 49 additions & 5 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "workflowai"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
description = ""
55
authors = ["Guillaume Aquilina <guillaume@workflowai.com>"]
66
readme = "README.md"

workflowai/core/fields/file.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,33 @@
1-
from pydantic import BaseModel, Field
1+
from typing import Optional
2+
3+
from pydantic import BaseModel, Field, model_validator
24

35

46
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+
)
825

926
def to_url(self) -> str:
1027
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
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytest
2+
from pydantic import ValidationError
3+
4+
from workflowai.core.fields.file import File
5+
6+
7+
class TestFile:
8+
def test_validate_data_or_url(self):
9+
with pytest.raises(ValidationError):
10+
File(data=None, url=None)
11+
12+
def test_with_valid_data(self):
13+
file = File(data="data", content_type="image/png")
14+
assert file.data == "data"
15+
assert file.content_type == "image/png"
16+
assert file.url is None
17+
18+
def test_with_valid_url(self):
19+
file = File(url="https://example.com/image.png")
20+
assert file.data is None
21+
assert file.content_type is None

0 commit comments

Comments
 (0)