Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Always stream OpenAI chat responses

Abi Rajaa5b644ff4348
diff --git a/backend/models/openai_client.py b/backend/models/openai_client.py
index 1acbfd8b..40b244e2 100644
--- a/backend/models/openai_client.py
+++ b/backend/models/openai_client.py
@@ -22,10 +22,8 @@ async def stream_openai_response(
"timeout": 600,
}
- # O1 doesn't support streaming or temperature
- if model_name not in ["o1-2024-12-17", "o4-mini-2025-04-16", "o3-2025-04-16"]:
- params["temperature"] = 0
- params["stream"] = True
+ params["temperature"] = 0
+ params["stream"] = True
# 4.1 series
if model_name in [
@@ -37,32 +35,19 @@ async def stream_openai_response(
params["stream"] = True
params["max_tokens"] = 20000
- if model_name == "o1-2024-12-17":
- params["max_completion_tokens"] = 20000
-
- if model_name in ["o4-mini-2025-04-16", "o3-2025-04-16"]:
- params["max_completion_tokens"] = 20000
- params["stream"] = True
- params["reasoning_effort"] = "high"
-
- # O1 doesn't support streaming
- if model_name == "o1-2024-12-17":
- response = await client.chat.completions.create(**params) # type: ignore
- full_response = response.choices[0].message.content # type: ignore
- else:
- stream = await client.chat.completions.create(**params) # type: ignore
- full_response = ""
- async for chunk in stream: # type: ignore
- assert isinstance(chunk, ChatCompletionChunk)
- if (
- chunk.choices
- and len(chunk.choices) > 0
- and chunk.choices[0].delta
- and chunk.choices[0].delta.content
- ):
- content = chunk.choices[0].delta.content or ""
- full_response += content
- await callback(content)
+ stream = await client.chat.completions.create(**params) # type: ignore
+ full_response = ""
+ async for chunk in stream: # type: ignore
+ assert isinstance(chunk, ChatCompletionChunk)
+ if (
+ chunk.choices
+ and len(chunk.choices) > 0
+ and chunk.choices[0].delta
+ and chunk.choices[0].delta.content
+ ):
+ content = chunk.choices[0].delta.content or ""
+ full_response += content
+ await callback(content)
await client.close()