Skip to content

Commit 9483957

Browse files
authored
Sentry: remove session passing to background job (#333)
1 parent d9a8545 commit 9483957

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

backend/app/api/routes/responses.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pydantic import BaseModel, Extra
88
from sqlmodel import Session
99

10+
from app.core.db import engine
1011
from app.api.deps import get_db, get_current_user_org_project
1112
from app.api.routes.threads import send_callback
1213
from app.crud.assistants import get_assistant_by_id
@@ -16,7 +17,7 @@
1617
get_ancestor_id_from_response,
1718
get_conversation_by_ancestor_id,
1819
)
19-
from app.models import UserProjectOrg, OpenAIConversationCreate
20+
from app.models import UserProjectOrg, OpenAIConversationCreate, OpenAIConversation
2021
from app.utils import APIResponse, mask_string
2122
from app.core.langfuse.langfuse import LangfuseTracer
2223

@@ -131,7 +132,8 @@ def process_response(
131132
tracer: LangfuseTracer,
132133
project_id: int,
133134
organization_id: int,
134-
session: Session,
135+
ancestor_id: str,
136+
latest_conversation: OpenAIConversation | None,
135137
):
136138
"""Process a response and send callback with results, with Langfuse tracing."""
137139
logger.info(
@@ -151,18 +153,6 @@ def process_response(
151153
)
152154

153155
try:
154-
# Get the latest conversation by ancestor ID to use as previous_response_id
155-
ancestor_id = request.response_id
156-
latest_conversation = None
157-
if ancestor_id:
158-
latest_conversation = get_conversation_by_ancestor_id(
159-
session=session,
160-
ancestor_response_id=ancestor_id,
161-
project_id=project_id,
162-
)
163-
if latest_conversation:
164-
ancestor_id = latest_conversation.response_id
165-
166156
params = {
167157
"model": assistant.model,
168158
"previous_response_id": ancestor_id,
@@ -211,35 +201,36 @@ def process_response(
211201
"error": None,
212202
},
213203
)
214-
# Set ancestor_response_id using CRUD function
215-
ancestor_response_id = (
216-
latest_conversation.ancestor_response_id
217-
if latest_conversation
218-
else get_ancestor_id_from_response(
219-
session=session,
220-
current_response_id=response.id,
221-
previous_response_id=response.previous_response_id,
222-
project_id=project_id,
204+
205+
with Session(engine) as session:
206+
ancestor_response_id = (
207+
latest_conversation.ancestor_response_id
208+
if latest_conversation
209+
else get_ancestor_id_from_response(
210+
session=session,
211+
current_response_id=response.id,
212+
previous_response_id=response.previous_response_id,
213+
project_id=project_id,
214+
)
223215
)
224-
)
225216

226-
# Create conversation record in database
227-
conversation_data = OpenAIConversationCreate(
228-
response_id=response.id,
229-
previous_response_id=response.previous_response_id,
230-
ancestor_response_id=ancestor_response_id,
231-
user_question=request.question,
232-
response=response.output_text,
233-
model=response.model,
234-
assistant_id=request.assistant_id,
235-
)
217+
# Create conversation record in database
218+
conversation_data = OpenAIConversationCreate(
219+
response_id=response.id,
220+
previous_response_id=response.previous_response_id,
221+
ancestor_response_id=ancestor_response_id,
222+
user_question=request.question,
223+
response=response.output_text,
224+
model=response.model,
225+
assistant_id=request.assistant_id,
226+
)
236227

237-
create_conversation(
238-
session=session,
239-
conversation=conversation_data,
240-
project_id=project_id,
241-
organization_id=organization_id,
242-
)
228+
create_conversation(
229+
session=session,
230+
conversation=conversation_data,
231+
project_id=project_id,
232+
organization_id=organization_id,
233+
)
243234

244235
request_dict = request.model_dump()
245236
callback_response = ResponsesAPIResponse.success_response(
@@ -346,6 +337,17 @@ async def responses(
346337
response_id=request.response_id,
347338
)
348339

340+
ancestor_id = request.response_id
341+
latest_conversation = None
342+
if ancestor_id:
343+
latest_conversation = get_conversation_by_ancestor_id(
344+
session=_session,
345+
ancestor_response_id=ancestor_id,
346+
project_id=project_id,
347+
)
348+
if latest_conversation:
349+
ancestor_id = latest_conversation.response_id
350+
349351
background_tasks.add_task(
350352
process_response,
351353
request,
@@ -354,7 +356,8 @@ async def responses(
354356
tracer,
355357
project_id,
356358
organization_id,
357-
_session,
359+
ancestor_id,
360+
latest_conversation,
358361
)
359362

360363
logger.info(

0 commit comments

Comments
 (0)