[Misc] Validate Cohere Embed Mixed Content Payloads (#45873)
Signed-off-by: Taneem Ibrahim <taneem.ibrahim@gmail.com>
This commit is contained in:
@@ -150,6 +150,16 @@ class TestCohereEmbedRequestParsing:
|
||||
{"content": [{"type": "text", "text": "hello"}]},
|
||||
],
|
||||
},
|
||||
{
|
||||
"model": "test",
|
||||
"inputs": [
|
||||
{
|
||||
"content": [
|
||||
{"type": "image_url", "image_url": {"url": "image-uri"}}
|
||||
]
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
)
|
||||
def test_accepts_exactly_one_non_empty_input_field(self, request_body):
|
||||
@@ -157,6 +167,38 @@ class TestCohereEmbedRequestParsing:
|
||||
|
||||
assert request.model == "test"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("content", "error"),
|
||||
[
|
||||
(
|
||||
{"type": "text"},
|
||||
"CohereEmbedContent with type='text' requires text",
|
||||
),
|
||||
(
|
||||
{"type": "image_url"},
|
||||
"CohereEmbedContent with type='image_url' requires image_url.url",
|
||||
),
|
||||
(
|
||||
{"type": "image_url", "image_url": {}},
|
||||
"CohereEmbedContent with type='image_url' requires image_url.url",
|
||||
),
|
||||
(
|
||||
{"type": "image_url", "image_url": {"url": ""}},
|
||||
"CohereEmbedContent with type='image_url' requires image_url.url",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_rejects_invalid_mixed_content_payloads(self, content, error):
|
||||
with pytest.raises(ValidationError, match=error):
|
||||
CohereEmbedRequest(
|
||||
model="test",
|
||||
inputs=[
|
||||
{
|
||||
"content": [content],
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
class TestResolveTruncation:
|
||||
"""Unit tests for EmbedIOProcessor._resolve_cohere_truncation."""
|
||||
|
||||
@@ -207,6 +207,17 @@ class CohereEmbedContent(BaseModel):
|
||||
text: str | None = None
|
||||
image_url: dict[str, str] | None = None
|
||||
|
||||
@model_validator(mode="after")
|
||||
def validate_content_payload(self):
|
||||
if self.type == "text":
|
||||
if self.text is None:
|
||||
raise ValueError("CohereEmbedContent with type='text' requires text")
|
||||
elif not self.image_url or not self.image_url.get("url"):
|
||||
raise ValueError(
|
||||
"CohereEmbedContent with type='image_url' requires image_url.url"
|
||||
)
|
||||
return self
|
||||
|
||||
|
||||
class CohereEmbedInput(BaseModel):
|
||||
content: list[CohereEmbedContent]
|
||||
|
||||
Reference in New Issue
Block a user