From 69477ca70c988923e5313115a2eb837b852bb780 Mon Sep 17 00:00:00 2001 From: sxhxliang Date: Mon, 5 Jan 2026 21:14:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DUnboundLocalError:=20cannot?= =?UTF-8?q?=20access=20local=20variable=20'json'=20where=20it=20is=20not?= =?UTF-8?q?=20associated=20=20=20with=20a=20value=20=E8=BF=99=E4=B8=AA=20b?= =?UTF-8?q?ug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/routes.py | 7 ++----- src/services/generation_handler.py | 1 - 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/api/routes.py b/src/api/routes.py index c515a1a..2cd4675 100644 --- a/src/api/routes.py +++ b/src/api/routes.py @@ -151,7 +151,6 @@ async def create_chat_completion( result = chunk if result: - import json return JSONResponse(content=json.loads(result)) else: return JSONResponse( @@ -169,7 +168,6 @@ async def create_chat_completion( # Handle streaming if request.stream: async def generate(): - import json as json_module # Import inside function to avoid scope issues try: async for chunk in generation_handler.handle_generation( model=request.model, @@ -184,7 +182,7 @@ async def create_chat_completion( # Try to parse structured error (JSON format) error_data = None try: - error_data = json_module.loads(str(e)) + error_data = json.loads(str(e)) except: pass @@ -202,7 +200,7 @@ async def create_chat_completion( "code": None } } - error_chunk = f'data: {json_module.dumps(error_response)}\n\n' + error_chunk = f'data: {json.dumps(error_response)}\n\n' yield error_chunk yield 'data: [DONE]\n\n' @@ -229,7 +227,6 @@ async def create_chat_completion( result = chunk if result: - import json return JSONResponse(content=json.loads(result)) else: # Return OpenAI-compatible error format diff --git a/src/services/generation_handler.py b/src/services/generation_handler.py index e8fb79e..81c94d8 100644 --- a/src/services/generation_handler.py +++ b/src/services/generation_handler.py @@ -600,7 +600,6 @@ class GenerationHandler: # Parse error message to check if it's a structured error (JSON) error_response = None try: - import json error_response = json.loads(str(e)) except: pass