From 7a122327375d20ceecc804bbd24b1d93254b1c92 Mon Sep 17 00:00:00 2001 From: Aviraj <100823015+avirajsingh7@users.noreply.github.com> Date: Thu, 3 Jul 2025 15:49:47 +0530 Subject: [PATCH 1/5] Remove project_id from request params in response API --- backend/app/api/routes/responses.py | 68 +++++++++++++---------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/backend/app/api/routes/responses.py b/backend/app/api/routes/responses.py index aba0244b..a2d1811f 100644 --- a/backend/app/api/routes/responses.py +++ b/backend/app/api/routes/responses.py @@ -1,6 +1,5 @@ import logging -import uuid -from typing import Any, Dict, Optional +from typing import Optional import openai from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException @@ -8,11 +7,11 @@ from pydantic import BaseModel, Extra from sqlmodel import Session -from app.api.deps import get_current_user_org, get_db +from app.api.deps import get_db, get_current_user_org_project from app.api.routes.threads import send_callback from app.crud.assistants import get_assistant_by_id from app.crud.credentials import get_provider_credential -from app.models import UserOrganization +from app.models import UserProjectOrg from app.utils import APIResponse from app.core.langfuse.langfuse import LangfuseTracer @@ -28,7 +27,6 @@ def handle_openai_error(e: openai.OpenAIError) -> str: class ResponsesAPIRequest(BaseModel): - project_id: int assistant_id: str question: str callback_url: Optional[str] = None @@ -39,7 +37,6 @@ class Config: class ResponsesSyncAPIRequest(BaseModel): - project_id: int model: str instructions: str vector_store_ids: list[str] @@ -91,8 +88,7 @@ def get_additional_data(request: dict) -> dict: return { k: v for k, v in request.items() - if k - not in {"project_id", "assistant_id", "callback_url", "response_id", "question"} + if k not in {"assistant_id", "callback_url", "response_id", "question"} } @@ -101,10 +97,11 @@ def process_response( client: OpenAI, assistant, tracer: LangfuseTracer, + project_id: int, ): """Process a response and send callback with results, with Langfuse tracing.""" logger.info( - f"Starting generating response for assistant_id={request.assistant_id}, project_id={request.project_id}" + f"Starting generating response for assistant_id={request.assistant_id}, project_id={project_id}" ) tracer.start_trace( @@ -143,7 +140,7 @@ def process_response( response_chunks = get_file_search_results(response) logger.info( - f"Successfully generated response: response_id={response.id}, assistant={request.assistant_id}, project_id={request.project_id}" + f"Successfully generated response: response_id={response.id}, assistant={request.assistant_id}, project_id={project_id}" ) tracer.end_generation( @@ -188,7 +185,7 @@ def process_response( except openai.OpenAIError as e: error_message = handle_openai_error(e) logger.error( - f"OpenAI API error during response processing: {error_message}, project_id={request.project_id}" + f"OpenAI API error during response processing: {error_message}, project_id={project_id}" ) tracer.log_error(error_message, response_id=request.response_id) callback_response = ResponsesAPIResponse.failure_response(error=error_message) @@ -197,11 +194,11 @@ def process_response( if request.callback_url: logger.info( - f"Sending callback to URL: {request.callback_url}, assistant={request.assistant_id}, project_id={request.project_id}" + f"Sending callback to URL: {request.callback_url}, assistant={request.assistant_id}, project_id={project_id}" ) send_callback(request.callback_url, callback_response.model_dump()) logger.info( - f"Callback sent successfully, assistant={request.assistant_id}, project_id={request.project_id}" + f"Callback sent successfully, assistant={request.assistant_id}, project_id={project_id}" ) @@ -210,32 +207,26 @@ async def responses( request: ResponsesAPIRequest, background_tasks: BackgroundTasks, _session: Session = Depends(get_db), - _current_user: UserOrganization = Depends(get_current_user_org), + _current_user: UserProjectOrg = Depends(get_current_user_org_project), ): - """Asynchronous endpoint that processes requests in background with Langfuse tracing.""" + project_id = _current_user.project_id + organization_id = _current_user.organization_id + logger.info( - f"Processing response request for assistant_id={request.assistant_id}, project_id={request.project_id}, organization_id={_current_user.organization_id}" + f"Processing response request for assistant_id={request.assistant_id}, project_id={project_id}" ) - assistant = get_assistant_by_id( - _session, request.assistant_id, _current_user.organization_id - ) + assistant = get_assistant_by_id(_session, request.assistant_id, organization_id) if not assistant: - logger.warning( - f"Assistant not found: assistant_id={request.assistant_id}, project_id={request.project_id}, organization_id={_current_user.organization_id}", - ) raise HTTPException(status_code=404, detail="Assistant not found or not active") credentials = get_provider_credential( session=_session, - org_id=_current_user.organization_id, + org_id=organization_id, provider="openai", - project_id=request.project_id, + project_id=project_id, ) if not credentials or "api_key" not in credentials: - logger.error( - f"OpenAI API key not configured for org_id={_current_user.organization_id}, project_id={request.project_id}" - ) return { "success": False, "error": "OpenAI API key not configured for this organization.", @@ -247,9 +238,9 @@ async def responses( langfuse_credentials = get_provider_credential( session=_session, - org_id=_current_user.organization_id, + org_id=organization_id, provider="langfuse", - project_id=request.project_id, + project_id=project_id, ) tracer = LangfuseTracer( credentials=langfuse_credentials, @@ -262,10 +253,11 @@ async def responses( client, assistant, tracer, + project_id, ) logger.info( - f"Background task scheduled for response processing: assistant_id={request.assistant_id}, project_id={request.project_id}, organization_id={_current_user.organization_id}" + f"Background task scheduled for response processing: assistant_id={request.assistant_id}, project_id={project_id}" ) return { @@ -284,14 +276,17 @@ async def responses( async def responses_sync( request: ResponsesSyncAPIRequest, _session: Session = Depends(get_db), - _current_user: UserOrganization = Depends(get_current_user_org), + _current_user: UserProjectOrg = Depends(get_current_user_org_project), ): """Synchronous endpoint for benchmarking OpenAI responses API with Langfuse tracing.""" + project_id = _current_user.project_id + organization_id = _current_user.organization_id + credentials = get_provider_credential( session=_session, - org_id=_current_user.organization_id, + org_id=organization_id, provider="openai", - project_id=request.project_id, + project_id=project_id, ) if not credentials or "api_key" not in credentials: return APIResponse.failure_response( @@ -302,9 +297,9 @@ async def responses_sync( langfuse_credentials = get_provider_credential( session=_session, - org_id=_current_user.organization_id, + org_id=organization_id, provider="langfuse", - project_id=request.project_id, + project_id=project_id, ) tracer = LangfuseTracer( credentials=langfuse_credentials, @@ -312,8 +307,7 @@ async def responses_sync( ) tracer.start_trace( - name="generate_response_sync", - input={"question": request.question}, + name="generate_response_sync", input={"question": request.question} ) tracer.start_generation( name="openai_response", From 2f517af28e284c316fe78e9ea4056dfba70f6261 Mon Sep 17 00:00:00 2001 From: Aviraj <100823015+avirajsingh7@users.noreply.github.com> Date: Thu, 3 Jul 2025 15:55:35 +0530 Subject: [PATCH 2/5] remove project_id from request in test cases --- backend/app/tests/api/routes/test_responses.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/backend/app/tests/api/routes/test_responses.py b/backend/app/tests/api/routes/test_responses.py index 257d2585..049b6dba 100644 --- a/backend/app/tests/api/routes/test_responses.py +++ b/backend/app/tests/api/routes/test_responses.py @@ -58,7 +58,6 @@ def test_responses_endpoint_success( headers = {"X-API-KEY": original_api_key} request_data = { - "project_id": glific_project.id, "assistant_id": "assistant_123", "question": "What is Glific?", "callback_url": "http://example.com/callback", @@ -118,7 +117,6 @@ def test_responses_endpoint_without_vector_store( headers = {"X-API-KEY": original_api_key} request_data = { - "project_id": glific_project.id, "assistant_id": "assistant_123", "question": "What is Glific?", "callback_url": "http://example.com/callback", From 89942e0ecfcf04bc0a87bdc4b0ca667fb7e91358 Mon Sep 17 00:00:00 2001 From: Aviraj <100823015+avirajsingh7@users.noreply.github.com> Date: Fri, 4 Jul 2025 10:59:44 +0530 Subject: [PATCH 3/5] refactor --- backend/app/api/routes/responses.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/backend/app/api/routes/responses.py b/backend/app/api/routes/responses.py index a2d1811f..6e24f9b9 100644 --- a/backend/app/api/routes/responses.py +++ b/backend/app/api/routes/responses.py @@ -209,8 +209,7 @@ async def responses( _session: Session = Depends(get_db), _current_user: UserProjectOrg = Depends(get_current_user_org_project), ): - project_id = _current_user.project_id - organization_id = _current_user.organization_id + project_id, organization_id = _current_user.project_id, _current_user.organization_id logger.info( f"Processing response request for assistant_id={request.assistant_id}, project_id={project_id}" @@ -279,8 +278,7 @@ async def responses_sync( _current_user: UserProjectOrg = Depends(get_current_user_org_project), ): """Synchronous endpoint for benchmarking OpenAI responses API with Langfuse tracing.""" - project_id = _current_user.project_id - organization_id = _current_user.organization_id + project_id, organization_id = _current_user.project_id, _current_user.organization_id credentials = get_provider_credential( session=_session, From ae4b1d2ffe1d10f67e3c0d3aa5bb748cfd4670ba Mon Sep 17 00:00:00 2001 From: Aviraj <100823015+avirajsingh7@users.noreply.github.com> Date: Mon, 7 Jul 2025 14:25:31 +0530 Subject: [PATCH 4/5] pre commit --- backend/app/api/routes/responses.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/app/api/routes/responses.py b/backend/app/api/routes/responses.py index 2d914971..5a9f2673 100644 --- a/backend/app/api/routes/responses.py +++ b/backend/app/api/routes/responses.py @@ -209,7 +209,10 @@ async def responses( _session: Session = Depends(get_db), _current_user: UserProjectOrg = Depends(get_current_user_org_project), ): - project_id, organization_id = _current_user.project_id, _current_user.organization_id + project_id, organization_id = ( + _current_user.project_id, + _current_user.organization_id, + ) logger.info( f"Processing response request for assistant_id={mask_string(request.assistant_id)}, project_id={project_id}, organization_id={organization_id}" @@ -229,6 +232,9 @@ async def responses( project_id=project_id, ) if not credentials or "api_key" not in credentials: + logger.error( + f"OpenAI API key not configured for org_id={organization_id}, project_id={project_id}" + ) return { "success": False, "error": "OpenAI API key not configured for this organization.", @@ -281,7 +287,10 @@ async def responses_sync( _current_user: UserProjectOrg = Depends(get_current_user_org_project), ): """Synchronous endpoint for benchmarking OpenAI responses API with Langfuse tracing.""" - project_id, organization_id = _current_user.project_id, _current_user.organization_id + project_id, organization_id = ( + _current_user.project_id, + _current_user.organization_id, + ) credentials = get_provider_credential( session=_session, From 22e09797552ce038c2857199b76083551d113587 Mon Sep 17 00:00:00 2001 From: Aviraj <100823015+avirajsingh7@users.noreply.github.com> Date: Tue, 8 Jul 2025 12:06:33 +0530 Subject: [PATCH 5/5] add docstring --- backend/app/api/routes/responses.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/app/api/routes/responses.py b/backend/app/api/routes/responses.py index 5a9f2673..233cab35 100644 --- a/backend/app/api/routes/responses.py +++ b/backend/app/api/routes/responses.py @@ -209,6 +209,8 @@ async def responses( _session: Session = Depends(get_db), _current_user: UserProjectOrg = Depends(get_current_user_org_project), ): + """Asynchronous endpoint that processes requests in background with Langfuse tracing.""" + project_id, organization_id = ( _current_user.project_id, _current_user.organization_id,