Skip to content

Commit d8e5a27

Browse files
authored
OpenAI Threads: Add Schema for validation (#245)
* add validation in thread/start * add thread id optional * precommit run
1 parent cff0579 commit d8e5a27

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

backend/app/api/routes/threads.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException
66
from openai import OpenAI
7+
from pydantic import BaseModel, Field
78
from sqlmodel import Session
9+
from typing import Optional
810
from langfuse.decorators import observe, langfuse_context
911

1012
from app.api.deps import get_current_user_org, get_db
@@ -19,6 +21,18 @@
1921
router = APIRouter(tags=["threads"])
2022

2123

24+
class StartThreadRequest(BaseModel):
25+
question: str = Field(..., description="The user's input question.")
26+
assistant_id: str = Field(..., description="The ID of the assistant to be used.")
27+
remove_citation: bool = Field(
28+
default=False, description="Whether to remove citations from the response."
29+
)
30+
thread_id: Optional[str] = Field(
31+
default=None,
32+
description="An optional existing thread ID to continue the conversation.",
33+
)
34+
35+
2236
def send_callback(callback_url: str, data: dict):
2337
"""Send results to the callback URL (synchronously)."""
2438
try:
@@ -340,14 +354,15 @@ async def threads_sync(
340354

341355
@router.post("/threads/start")
342356
async def start_thread(
343-
request: dict,
357+
request: StartThreadRequest,
344358
background_tasks: BackgroundTasks,
345359
db: Session = Depends(get_db),
346360
_current_user: UserOrganization = Depends(get_current_user_org),
347361
):
348362
"""
349363
Create a new OpenAI thread for the given question and start polling in the background.
350364
"""
365+
request = request.model_dump()
351366
prompt = request["question"]
352367
credentials = get_provider_credential(
353368
session=db,

0 commit comments

Comments
 (0)