Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(api): return 400 for invalid multimodal field types
FanouZeng-TT96ad7b4dbf61
diff --git a/nanobot/api/server.py b/nanobot/api/server.pyindex ce9ed38e0..f18e03d8b 100644--- a/nanobot/api/server.py+++ b/nanobot/api/server.py@@ -297,7 +297,7 @@ async def handle_chat_completions(request: web.Request) -> web.Response | web.Strequested_model = body.get("model")text, media_paths = _parse_json_content(body)session_id = body.get("session_id")- except ValueError as e:+ except (TypeError, ValueError) as e:return _error_json(400, str(e))except _FileSizeExceeded as e:return _error_json(413, str(e), err_type="invalid_request_error")diff --git a/tests/test_openai_api.py b/tests/test_openai_api.pyindex 7675a9ec2..48cc41490 100644--- a/tests/test_openai_api.py+++ b/tests/test_openai_api.py@@ -468,6 +468,50 @@ async def test_multimodal_content_extracts_text(aiohttp_client, mock_agent) -> Nassert len(call_kwargs.get("media") or []) >= 0 # base64 images saved to disk[email protected](not HAS_AIOHTTP, reason="aiohttp not installed")+ ("content_part", "expected_error"),+ [+ ({"type": "text", "text": 123}, "messages[0].content[].text must be a string"),+ (+ {"type": "image_url", "image_url": "not-an-object"},+ "messages[0].content[].image_url must be an object",+ ),+ (+ {"type": "image_url", "image_url": {"url": 123}},+ "messages[0].content[].image_url.url must be a string",+ ),+ ],+)+async def test_multimodal_invalid_field_type_returns_400(+ aiohttp_client,+ mock_agent,+ content_part: dict[str, object],+ expected_error: str,+) -> None:+ app = create_app(mock_agent, model_name="m", api_key=API_KEY)+ client = await aiohttp_client(app)+ resp = await client.post(+ "/v1/chat/completions",+ headers=AUTH_HEADERS,+ json={+ "messages": [+ {+ "role": "user",+ "content": [content_part],+ }+ ]+ },+ )++ assert resp.status == 400+ body = await resp.json()+ assert body["error"]["message"] == expected_error+ assert body["error"]["code"] == 400+ mock_agent.process_direct.assert_not_called()++@pytest.mark.skipif(not HAS_AIOHTTP, reason="aiohttp not installed")@pytest.mark.asyncioasync def test_multimodal_remote_image_url_returns_400(aiohttp_client, mock_agent) -> None: