Commit 70a6cfe1 authored by Your Name's avatar Your Name

Fix UnboundLocalError for StreamingResponse in chat_completions

The issue was caused by importing StreamingResponse and JSONResponse inside
the chat_completions function. In Python, when you have an import statement
anywhere inside a function, it creates a local variable for that name
throughout the entire function scope. This caused the code in the original
implementation path to fail because Python saw StreamingResponse as an
unassigned local variable.

Fix: Move StreamingResponse and JSONResponse imports to module level and
remove redundant imports from inside the function.
parent 6c2c0afc
......@@ -23,7 +23,7 @@ from typing import AsyncGenerator, Dict, List, Optional, Union
import psutil
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import StreamingResponse, FileResponse
from fastapi.responses import StreamingResponse, FileResponse, JSONResponse
from pydantic import BaseModel, Field, validator, field_validator, ConfigDict
from pydantic_core import PydanticCustomError
from threading import Thread
......@@ -5292,7 +5292,6 @@ async def chat_completions(request: ChatCompletionRequest, http_request: Request
try:
if request.stream:
# Streaming response
from fastapi.responses import StreamingResponse
async def generate():
try:
......@@ -5373,8 +5372,6 @@ async def chat_completions(request: ChatCompletionRequest, http_request: Request
completion_tokens=response.get('usage', {}).get('completion_tokens', 0)
)
from fastapi.responses import JSONResponse
return JSONResponse(content=response, headers=headers)
except Exception as e:
# Handle litellm errors
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment