1818logger = logging .getLogger (__name__ )
1919router = APIRouter (tags = ["threads" ])
2020
21+ from pydantic import BaseModel
22+ from typing import Optional
23+
2124
2225def send_callback (callback_url : str , data : dict ):
23- """Send results to the callback URL (synchronously )."""
26+ """Send results to the callback URL (synchron ously )."""
2427 try :
2528 session = requests .Session ()
2629 # uncomment this to run locally without SSL
@@ -222,6 +225,8 @@ async def threads(
222225 _current_user : UserOrganization = Depends (get_current_user_org ),
223226):
224227 """Asynchronous endpoint that processes requests in background."""
228+
229+ # Fetch OpenAI credentials (required)
225230 credentials = get_provider_credential (
226231 session = _session ,
227232 org_id = _current_user .organization_id ,
@@ -234,22 +239,22 @@ async def threads(
234239 )
235240 client = OpenAI (api_key = credentials ["api_key" ])
236241
242+ # Fetch Langfuse credentials (optional)
237243 langfuse_credentials = get_provider_credential (
238244 session = _session ,
239245 org_id = _current_user .organization_id ,
240246 provider = "langfuse" ,
241247 project_id = request .get ("project_id" ),
242248 )
243- if not langfuse_credentials :
244- return APIResponse .failure_response (
245- error = "LANGFUSE keys not configured for this organization."
249+
250+ # If Langfuse credentials exist, configure Langfuse
251+ if langfuse_credentials :
252+ langfuse_context .configure (
253+ secret_key = langfuse_credentials ["secret_key" ],
254+ public_key = langfuse_credentials ["public_key" ],
255+ host = langfuse_credentials ["host" ],
246256 )
247257
248- langfuse_context .configure (
249- secret_key = langfuse_credentials ["secret_key" ],
250- public_key = langfuse_credentials ["public_key" ],
251- host = langfuse_credentials ["host" ],
252- )
253258 # Validate thread
254259 is_valid , error_message = validate_thread (client , request .get ("thread_id" ))
255260 if not is_valid :
@@ -350,15 +355,29 @@ async def start_thread(
350355 """
351356 Create a new OpenAI thread for the given question and start polling in the background.
352357 """
353- prompt = request ["question" ]
354- client = OpenAI (api_key = settings .OPENAI_API_KEY )
358+ # Fetch OpenAI credentials (required)
359+ openai_credentials = get_provider_credential (
360+ session = db ,
361+ org_id = _current_user .organization_id ,
362+ provider = "openai" ,
363+ project_id = request .get ("project_id" ),
364+ )
365+
366+ if not openai_credentials or "api_key" not in openai_credentials :
367+ return APIResponse .failure_response (
368+ error = "OpenAI API key not configured for this organization."
369+ )
355370
371+ client = OpenAI (api_key = openai_credentials ["api_key" ])
372+ # Setup thread
356373 is_success , error = setup_thread (client , request )
357374 if not is_success :
358375 return APIResponse .failure_response (error = error )
359376
360377 thread_id = request ["thread_id" ]
378+ prompt = request ["question" ]
361379
380+ # Insert thread data into the database
362381 upsert_thread_result (
363382 db ,
364383 OpenAIThreadCreate (
@@ -370,6 +389,7 @@ async def start_thread(
370389 ),
371390 )
372391
392+ # Schedule background task
373393 background_tasks .add_task (poll_run_and_prepare_response , request , client , db )
374394
375395 return APIResponse .success_response (
0 commit comments