Skip to content

Commit b91d630

Browse files
authored
fix: processing errors for streaming requests (#5) (#6)
1 parent b5cc836 commit b91d630

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

aidial_adapter_openai/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ async def chat_completion(deployment_id: str, request: Request):
6060
)
6161

6262
if is_stream:
63+
if isinstance(response, Response):
64+
return response
65+
6366
return StreamingResponse(
6467
generate_stream(
6568
data["messages"],

tests/test_errors.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ async def test_incorrect_upstream_url(aioresponses: aioresponses):
114114

115115

116116
@pytest.mark.asyncio
117-
async def test_asdasd(aioresponses: aioresponses):
117+
async def test_incorrect_format(aioresponses: aioresponses):
118118
aioresponses.post(
119119
"http://localhost:5001/openai/deployments/gpt-4/chat/completions?api-version=2023-03-15-preview",
120120
status=400,
@@ -134,3 +134,46 @@ async def test_asdasd(aioresponses: aioresponses):
134134
assert response.status_code == 400
135135

136136
assert response.content == b"Incorrect format"
137+
138+
139+
@pytest.mark.asyncio
140+
async def test_incorrect_streaming_request(aioresponses: aioresponses):
141+
aioresponses.post(
142+
"http://localhost:5001/openai/deployments/gpt-4/chat/completions?api-version=2023-03-15-preview",
143+
status=400,
144+
body=json.dumps(
145+
{
146+
"error": {
147+
"message": "0 is less than the minimum of 1 - 'n'",
148+
"type": "invalid_request_error",
149+
"param": None,
150+
"code": None,
151+
}
152+
}
153+
),
154+
content_type="application/json",
155+
)
156+
test_app = AsyncClient(app=app, base_url="http://test.com")
157+
158+
response = await test_app.post(
159+
"/openai/deployments/gpt-4/chat/completions",
160+
json={
161+
"messages": [{"role": "user", "content": "Test content"}],
162+
"stream": True,
163+
"n": 0,
164+
},
165+
headers={
166+
"X-UPSTREAM-KEY": "TEST_API_KEY",
167+
"X-UPSTREAM-ENDPOINT": "http://localhost:5001/openai/deployments/gpt-4/chat/completions",
168+
},
169+
)
170+
171+
assert response.status_code == 400
172+
assert response.json() == {
173+
"error": {
174+
"message": "0 is less than the minimum of 1 - 'n'",
175+
"type": "invalid_request_error",
176+
"param": None,
177+
"code": None,
178+
}
179+
}

0 commit comments

Comments
 (0)