Skip to content

Commit f7b84db

Browse files
committed
Add QueryRequest.media_type for backwards compatibility.
1 parent e5de290 commit f7b84db

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/models/requests.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
from pydantic import BaseModel, model_validator, field_validator, Field
66
from llama_stack_client.types.agents.turn_create_params import Document
77

8+
from log import get_logger
89
from utils import suid
910

11+
logger = get_logger(__name__)
12+
1013

1114
class Attachment(BaseModel):
1215
"""Model representing an attachment that can be send from UI as part of query.
@@ -55,8 +58,6 @@ class Attachment(BaseModel):
5558
}
5659

5760

58-
# TODO(lucasagomes): add media_type when needed, current implementation
59-
# does not support streaming response, so this is not used
6061
class QueryRequest(BaseModel):
6162
"""Model representing a request for the LLM (Language Model).
6263
@@ -80,6 +81,9 @@ class QueryRequest(BaseModel):
8081
model: Optional[str] = None
8182
system_prompt: Optional[str] = None
8283
attachments: Optional[list[Attachment]] = None
84+
# media_type is not used in 'lightspeed-stack' that only supports application/json.
85+
# the field is kept here to enable compatibility with 'road-core' clients.
86+
media_type: Optional[str] = None
8387

8488
# provides examples for /docs endpoint
8589
model_config = {
@@ -132,6 +136,15 @@ def validate_provider_and_model(self) -> Self:
132136
raise ValueError("Model must be specified if provider is specified")
133137
return self
134138

139+
@model_validator(mode="after")
140+
def validate_media_type(self) -> Self:
141+
"""Log use of media_type that is unsupported but kept for backwards compatibility."""
142+
if self.media_type:
143+
logger.warning(
144+
"media_type was set in the request but is not supported. The value will be ignored."
145+
)
146+
return self
147+
135148

136149
class FeedbackRequest(BaseModel):
137150
"""Model representing a feedback request.

tests/unit/models/test_requests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""Test the QueryRequest, Attachment, and FeedbackRequest models."""
22

3+
from unittest.mock import Mock
4+
from logging import Logger
5+
36
import pytest
47
from pydantic import ValidationError
58
from models.requests import QueryRequest, Attachment, FeedbackRequest
@@ -143,6 +146,28 @@ def test_validate_provider_and_model(self) -> None:
143146
):
144147
QueryRequest(query="Tell me about Kubernetes", provider="OpenAI")
145148

149+
def test_validate_media_type(self, mocker) -> None:
150+
"""Test the validate_media_type method."""
151+
152+
# Mock the logger
153+
mock_logger = Mock(spec=Logger)
154+
mocker.patch("models.requests.logger", mock_logger)
155+
156+
qr = QueryRequest(
157+
query="Tell me about Kubernetes",
158+
provider="OpenAI",
159+
model="gpt-3.5-turbo",
160+
media_type="text/plain",
161+
)
162+
assert qr is not None
163+
assert qr.provider == "OpenAI"
164+
assert qr.model == "gpt-3.5-turbo"
165+
assert qr.media_type == "text/plain"
166+
167+
mock_logger.warning.assert_called_once_with(
168+
"media_type was set in the request but is not supported. The value will be ignored."
169+
)
170+
146171

147172
class TestFeedbackRequest:
148173
"""Test cases for the FeedbackRequest model."""

0 commit comments

Comments
 (0)