修复UnboundLocalError: cannot access local variable 'json' where it is not associated

with a value 这个 bug
This commit is contained in:
sxhxliang
2026-01-05 21:14:19 +08:00
parent b6d272fcfb
commit 69477ca70c
2 changed files with 2 additions and 6 deletions

View File

@@ -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

View File

@@ -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