Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
fix(server): return plain text for response_format=text
David-Wu111932c9ebb05932
diff --git a/funasr/bin/_server_app.py b/funasr/bin/_server_app.pyindex 588f1fbb1..8822e94a4 100644--- a/funasr/bin/_server_app.py+++ b/funasr/bin/_server_app.py@@ -20,7 +20,7 @@ import soundfile as sftry:from fastapi import FastAPI, UploadFile, File, Form, HTTPExceptionfrom fastapi.middleware.cors import CORSMiddleware- from fastapi.responses import JSONResponse+ from fastapi.responses import JSONResponse, PlainTextResponseexcept ImportError:raise ImportError("funasr-server requires additional packages. Install with: pip install vllm fastapi uvicorn python-multipart"@@ -529,7 +529,7 @@ def create_app(if response_format == "verbose_json":return JSONResponse(build_openai_verbose_json(result, requested_language=language))elif response_format == "text":- return JSONResponse(result["text"])+ return PlainTextResponse(result["text"])else:return JSONResponse({"text": result["text"]})diff --git a/tests/test_server_app_openai_segments.py b/tests/test_server_app_openai_segments.pyindex 06a4951f8..28df9bc6e 100644--- a/tests/test_server_app_openai_segments.py+++ b/tests/test_server_app_openai_segments.py@@ -12,6 +12,11 @@ SERVICE_PATH = REPO_ROOT / "funasr" / "bin" / "_server_app.py"SERVER_CLI_PATH = REPO_ROOT / "funasr" / "bin" / "server.py"+class PlainTextResponseStub:+ def __init__(self, content=None):+ self.content = content++def load_server_app(monkeypatch):class DummyFastAPI:def __init__(self, *args, **kwargs):@@ -57,6 +62,7 @@ def load_server_app(monkeypatch):responses_stub = types.ModuleType("fastapi.responses")responses_stub.JSONResponse = lambda content=None: content+ responses_stub.PlainTextResponse = PlainTextResponseStubmonkeypatch.setitem(sys.modules, "fastapi", fastapi_stub)monkeypatch.setitem(sys.modules, "fastapi.middleware", middleware_stub)@@ -260,6 +266,28 @@ def test_n8n_whisper_model_alias_uses_preloaded_model(monkeypatch):]+def test_text_response_format_returns_plain_text(monkeypatch):+ module = load_server_app(monkeypatch)+ install_dummy_funasr(monkeypatch, generated_text='say "hi"')+ monkeypatch.setattr(module.sf, "info", lambda path: types.SimpleNamespace(duration=1.25))+ app = module.create_app(device="cpu", preload_model="sensevoice")+ transcribe = app.routes[("POST", "/v1/audio/transcriptions")]++ response = asyncio.run(+ transcribe(+ file=DummyUpload(),+ model="sensevoice",+ language=None,+ response_format="text",+ spk=False,+ )+ )++ # OpenAI returns the bare transcript, not a JSON-encoded string.+ assert isinstance(response, PlainTextResponseStub)+ assert response.content == 'say "hi"'++def test_openai_verbose_json_preserves_speaker_labels(monkeypatch):module = load_server_app(monkeypatch)