💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Fix msgpack Accept header crashing streaming API endpoints (#1144)
Andrew Chen597ba8133d7b
diff --git a/src/python/txtai/api/route.py b/src/python/txtai/api/route.pyindex 68a23d49..cc74358f 100644--- a/src/python/txtai/api/route.py+++ b/src/python/txtai/api/route.py@@ -4,6 +4,7 @@ Route moduleimport json+from fastapi.responses import StreamingResponsefrom fastapi.routing import APIRoutefrom .responses import ResponseFactory, MessagePackResponse@@ -40,7 +41,7 @@ class EncodingAPIRoute(APIRoute):status_code=response.status_code,headers={k: v for k, v in response.headers.items() if k.lower() not in ["content-length", "content-type"]},)- if not isinstance(response, target) and target == MessagePackResponse+ if not isinstance(response, (target, StreamingResponse)) and target == MessagePackResponseelse response)diff --git a/test/python/testapi/testencoding.py b/test/python/testapi/testencoding.pyindex 1b7cfe99..1133f6f2 100644--- a/test/python/testapi/testencoding.py+++ b/test/python/testapi/testencoding.py@@ -121,6 +121,34 @@ class TestEncoding(unittest.TestCase):# Test reading imageself.assertIsInstance(PIL.Image.open(BytesIO(results[0]["object"])), PIL.Image.Image)+ def testMessagePackStreaming(self):+ """+ Test a streaming response is passed through unchanged when msgpack is requested+ """++ from fastapi import FastAPI+ from fastapi.responses import StreamingResponse++ from txtai.api.route import EncodingAPIRoute++ app = FastAPI()+ app.router.route_class = EncodingAPIRoute++ @app.get("/stream")+ def stream():+ def generate():+ yield b"hello"+ yield b"world"++ return StreamingResponse(generate())++ client = TestClient(app)++ # A msgpack Accept header on a streaming endpoint must not try to re-wrap it (no .body)+ response = client.get("stream", headers={"Accept": "application/msgpack"})+ self.assertEqual(response.status_code, 200)+ self.assertEqual(response.content, b"helloworld")+def testObjects(self):"""Test object encoding