diff --git a/backend/app/api/routes/threads.py b/backend/app/api/routes/threads.py index 2019d0e85..818d2db08 100644 --- a/backend/app/api/routes/threads.py +++ b/backend/app/api/routes/threads.py @@ -20,6 +20,9 @@ logger = logging.getLogger(__name__) router = APIRouter(tags=["threads"]) +from pydantic import BaseModel +from typing import Optional + class StartThreadRequest(BaseModel): question: str = Field(..., description="The user's input question.") @@ -243,6 +246,8 @@ async def threads( _current_user: UserOrganization = Depends(get_current_user_org), ): """Asynchronous endpoint that processes requests in background.""" + + # Fetch OpenAI credentials (required) credentials = get_provider_credential( session=_session, org_id=_current_user.organization_id, @@ -251,6 +256,9 @@ async def threads( ) client, success = configure_openai(credentials) if not success: + logging.error( + f"Failed to configure OpenAI API for organization { _current_user.organization_id }" + ) return APIResponse.failure_response( error="OpenAI API key not configured for this organization." ) @@ -261,15 +269,17 @@ async def threads( provider="langfuse", project_id=request.get("project_id"), ) - if not langfuse_credentials: - raise HTTPException(404, "LANGFUSE keys not configured for this organization.") - # Configure Langfuse - _, success = configure_langfuse(langfuse_credentials) - if not success: - return APIResponse.failure_response( - error="Failed to configure Langfuse client." + if langfuse_credentials: + logging.info( + f"Langfuse credentials found for organization {_current_user.organization_id}. Enabling tracing." + ) + _, success = configure_langfuse(langfuse_credentials, enabled=True) + else: + logging.info( + f"Langfuse credentials not found for organization {_current_user.organization_id}. Disabling tracing." ) + _, success = configure_langfuse({}, enabled=False) # Validate thread is_valid, error_message = validate_thread(client, request.get("thread_id")) diff --git a/backend/app/core/util.py b/backend/app/core/util.py index c3cd4c934..eb35b2f70 100644 --- a/backend/app/core/util.py +++ b/backend/app/core/util.py @@ -37,37 +37,50 @@ def post_callback(url: HttpUrl, payload: BaseModel): return not errno -def configure_langfuse(credentials: dict) -> tuple[Langfuse, bool]: +def configure_langfuse(credentials: dict, enabled: bool) -> tuple[Langfuse, bool]: """ - Configure Langfuse client and context with the provided credentials. + Configure Langfuse client and context with the provided credentials and tracing status. Args: credentials: Dictionary containing Langfuse credentials (public_key, secret_key, host) + enabled: Boolean to enable/disable Langfuse tracing. Returns: Tuple of (Langfuse client instance, success boolean) """ - if not credentials: - return None, False - try: - # Configure Langfuse client - langfuse = Langfuse( - public_key=credentials["public_key"], - secret_key=credentials["secret_key"], - host=credentials.get("host", "https://cloud.langfuse.com"), - ) - - # Configure Langfuse context - langfuse_context.configure( - secret_key=credentials["secret_key"], - public_key=credentials["public_key"], - host=credentials.get("host", "https://cloud.langfuse.com"), - ) + if credentials: + # If credentials exist, configure Langfuse client and enable tracing + # Configure Langfuse client + langfuse = Langfuse( + public_key=credentials["public_key"], + secret_key=credentials["secret_key"], + host=credentials.get("host", "https://cloud.langfuse.com"), + ) + + # Configure Langfuse context with tracing status + langfuse_context.configure( + secret_key=credentials["secret_key"], + public_key=credentials["public_key"], + host=credentials.get("host", "https://cloud.langfuse.com"), + enabled=enabled, + ) + + return langfuse, True + + else: + # If no credentials, disable tracing + # Disable Langfuse context if no credentials are provided + langfuse_context.configure( + secret_key=None, + public_key=None, + host=None, + enabled=False, + ) + + return None, False - return langfuse, True except Exception as e: - warnings.warn(f"Failed to configure Langfuse: {str(e)}") return None, False