Add Long-Running Responses API Agent Template#146
Add Long-Running Responses API Agent Template#146david-tempelmann wants to merge 5 commits intodatabricks:mainfrom
Conversation
|
@bbqiu This is my PR. The current e2e-chatbot-app-next won't work with this agent and would require some changes. The corresponding client contract is defined in the README.md |
bbqiu
left a comment
There was a problem hiding this comment.
this looks great! i'll go over this again tmrw to fix some small things after comments are addressed!
|
|
||
|
|
||
| @invoke() | ||
| async def invoke(request: ResponsesAgentRequest) -> ResponsesAgentResponse: |
There was a problem hiding this comment.
small nit to rename to invoke_handler / stream_handler
There was a problem hiding this comment.
should be able to just steal this file from the openai agents SDK from main btw
|
|
||
|
|
||
| @stream() | ||
| async def stream(request: dict) -> AsyncGenerator[ResponsesAgentStreamEvent, None]: |
There was a problem hiding this comment.
small nit to fix this type hint
| def _sse_event(event_type: str, data: dict[str, Any] | str) -> str: | ||
| """Format an SSE event per Open Responses spec: event must match type in body.""" | ||
| payload = data if isinstance(data, str) else json.dumps(data) | ||
| return f"event: {event_type}\ndata: {payload}\n\n" |
There was a problem hiding this comment.
ooc, how did the frontend client handle this?
There was a problem hiding this comment.
I did not change anything in addition to what I initially implemented to make background mode work. It still worked but I would need to check in detail how the frontend handles them.
| last_output_index: int = -1 | ||
|
|
||
|
|
||
| def _normalize_stream_event( |
There was a problem hiding this comment.
ah were these the restrictions we had to get around to make it work with the .stream from the responses client? if so, we can maybe drop these requirements for now, as this seems a tad brittle
needing to remap output_index etc. is quite unfortunate, and it's a bit confusing that the openai-agents sdk doesn't produce output that is compatible w/ the client itself
| """ | ||
| super()._setup_routes() | ||
|
|
||
| # TODO: check because I don't think we need pghost ... just the LAKEBASE_INSTANCE_NAME |
There was a problem hiding this comment.
as an FYI the frontend template requires pghost for the stateful chats
There was a problem hiding this comment.
ack. But that requirement should not be handled/checked in for the agent server I guess? I just simplified the warning message and removed the TODO.
| } | ||
|
|
||
| if is_streaming: | ||
| asyncio.create_task( |
There was a problem hiding this comment.
nit: should we have a default timeout that's configurable of 30 min? just so stuff doesn't run forever
|
|
||
| #### Implementing with the OpenAI SDK | ||
|
|
||
| ```mermaid |
There was a problem hiding this comment.
i think there's a syntax error with this mermaid diagram
agent-openai-agents-sdk-long-running-agenttemplate for long-running agent queries (minutes instead of seconds).background: truereturns immediately; client polls GET until completion. (2) Background + Stream – POST withstream: true,background: truereturns an SSE stream; if the connection drops, client resumes viaGET /responses/{id}?stream=true&starting_after=Nto receive remaining events from sequence N+1.demo_long_running_agent.pyscript to demonstrate how to interact with the agent using the OpenAI agents sdk. The script uses a short and a long dummy query for demo purposes. The long query is supposed to run beyond the 120 second timeout to demonstrate stream resumption.