fix: Improve cancellation for OpenAI response streams - #8937
Conversation
Greptile SummaryThe PR centralizes best-effort cancellation of Triton inference when OpenAI chat or completion streams terminate early.
Confidence Score: 5/5The PR appears safe to merge because no blocking failure remains. No blocking failure remains. Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant FastAPI
participant Engine
participant Triton
Client->>FastAPI: Streaming chat/completion request
FastAPI->>Engine: chat() / completion()
Engine->>Triton: async_infer()
Triton-->>Engine: Response iterator
Engine-->>FastAPI: Wrapped async stream
FastAPI-->>Client: SSE chunks
alt Stream completes normally
Engine-->>Client: Final chunk and [DONE]
else Stream terminates early
Engine->>Triton: cancel()
Engine->>Engine: Close inner stream
end
Reviews (6): Last reviewed commit: "Merge branch 'main' into spolisetty/tri-..." | Re-trigger Greptile |
| stream = self._streaming_completion_iterator( | ||
| request_id, created, request, responses, metadata.backend | ||
| ) | ||
| return self._stream_with_inference_cancellation( |
There was a problem hiding this comment.
_stream_with_inference_cancellation returns AsynsGenerator, not matching the return type of completion function. How does it work?
There was a problem hiding this comment.
completion() is awaited by the router. For a streaming request, the result of that await is the AsyncGenerator returned by _stream_with_inference_cancellation, which is then consumed by StreamingResponse.
AsyncGenerator[str, None] is a subtype of AsyncIterator[str], so it satisfies the declared return type of completion(). The shared LLMEngine interface has also been updated to declare these methods as asynchronous and use AsyncIterator[str] for streaming results.
…os-via-silent-async-cancellation
…os-via-silent-async-cancellation
…os-via-silent-async-cancellation
What does the PR do?
This PR ensures that Triton inference is cancelled when an OpenAI chat or completion stream terminates before completion. It centralizes best-effort cancellation and error reporting, while adding backend-observed tests for tool-call truncation and abandoned streams.
Checklist
<commit_type>: <Title>Commit Type:
Check the conventional commit type
box here and add the label to the github PR.
Related PRs:
Where should the reviewer start?
Test plan:
Caveats:
Background
Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)