22import openai
33import requests
44
5- from fastapi import APIRouter , BackgroundTasks , Depends
5+ from fastapi import APIRouter , BackgroundTasks , Depends , HTTPException
66from openai import OpenAI
77from sqlmodel import Session
88from langfuse .decorators import observe , langfuse_context
@@ -229,9 +229,8 @@ async def threads(
229229 project_id = request .get ("project_id" ),
230230 )
231231 if not credentials or "api_key" not in credentials :
232- return APIResponse .failure_response (
233- error = "OpenAI API key not configured for this organization."
234- )
232+ raise HTTPException (404 , "OpenAI API key not configured for this organization." )
233+
235234 client = OpenAI (api_key = credentials ["api_key" ])
236235
237236 langfuse_credentials = get_provider_credential (
@@ -241,9 +240,7 @@ async def threads(
241240 project_id = request .get ("project_id" ),
242241 )
243242 if not langfuse_credentials :
244- return APIResponse .failure_response (
245- error = "LANGFUSE keys not configured for this organization."
246- )
243+ raise HTTPException (404 , "LANGFUSE keys not configured for this organization." )
247244
248245 langfuse_context .configure (
249246 secret_key = langfuse_credentials ["secret_key" ],
@@ -253,12 +250,11 @@ async def threads(
253250 # Validate thread
254251 is_valid , error_message = validate_thread (client , request .get ("thread_id" ))
255252 if not is_valid :
256- return APIResponse .failure_response (error = error_message )
257-
253+ raise Exception (error_message )
258254 # Setup thread
259255 is_success , error_message = setup_thread (client , request )
260256 if not is_success :
261- return APIResponse . failure_response ( error = error_message )
257+ raise Exception ( error_message )
262258
263259 # Send immediate response
264260 initial_response = APIResponse .success_response (
@@ -291,21 +287,20 @@ async def threads_sync(
291287 project_id = request .get ("project_id" ),
292288 )
293289 if not credentials or "api_key" not in credentials :
294- return APIResponse . failure_response (
295- error = "OpenAI API key not configured for this organization."
290+ raise HTTPException (
291+ 404 , error = "OpenAI API key not configured for this organization."
296292 )
297293
298294 client = OpenAI (api_key = credentials ["api_key" ])
299295
300296 # Validate thread
301297 is_valid , error_message = validate_thread (client , request .get ("thread_id" ))
302298 if not is_valid :
303- return APIResponse .failure_response (error = error_message )
304-
299+ raise Exception (error_message )
305300 # Setup thread
306301 is_success , error_message = setup_thread (client , request )
307302 if not is_success :
308- return APIResponse . failure_response ( error = error_message )
303+ raise Exception ( error_message )
309304
310305 try :
311306 # Process run
@@ -337,7 +332,7 @@ async def threads_sync(
337332 )
338333
339334 except openai .OpenAIError as e :
340- return APIResponse . failure_response (error = handle_openai_error (e ))
335+ raise Exception (error = handle_openai_error (e ))
341336
342337
343338@router .post ("/threads/start" )
@@ -355,7 +350,7 @@ async def start_thread(
355350
356351 is_success , error = setup_thread (client , request )
357352 if not is_success :
358- return APIResponse . failure_response ( error = error )
353+ raise Exception ( error )
359354
360355 thread_id = request ["thread_id" ]
361356
@@ -394,7 +389,7 @@ async def get_thread(
394389 result = get_thread_result (db , thread_id )
395390
396391 if not result :
397- return APIResponse . failure_response ( error = "Thread not found. " )
392+ raise HTTPException ( 404 , "thread not found" )
398393
399394 status = result .status or ("success" if result .response else "processing" )
400395
0 commit comments