-
Notifications
You must be signed in to change notification settings - Fork 3.5k
fix(inference): keep a stream retryable until it emits generation #6697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
u9g
wants to merge
5
commits into
main
Choose a base branch
from
fix/llm-retryable-content-only
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
16b8ac5
fix(inference): keep a stream retryable until it emits generation
u9g 208de99
fix(inference): report a stalled stream body as a timeout, not a conn…
u9g 4306a0c
refactor(llm): name the test for whether a chunk carries generation
u9g 2cfbed9
fix(llm): measure ttft against generation, not the first chunk to arrive
u9g a2221bd
fix(openai): keep a Responses stream retryable until it emits generation
u9g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| """Retry eligibility, and reported latency, of a failed inference LLM stream. | ||
|
|
||
| A stream that dies having emitted nothing the caller can see must be retried: | ||
| provider metadata (a gateway deployment stamp, a thought signature) and token | ||
| counts are not generation. The same line divides the latency the caller actually | ||
| waited from the moment a contentless chunk happened to arrive. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from collections.abc import AsyncIterator, Callable | ||
|
|
||
| import httpx | ||
| import openai | ||
| import pytest | ||
|
|
||
| from livekit.agents import APIConnectOptions, APITimeoutError, llm | ||
| from livekit.agents.inference import LLM | ||
| from livekit.agents.metrics import LLMMetrics | ||
|
|
||
| pytestmark = pytest.mark.unit | ||
|
|
||
|
|
||
| def _sse(payload: dict) -> bytes: | ||
| return f"data: {json.dumps(payload)}\n\n".encode() | ||
|
|
||
|
|
||
| def _chunk(delta: dict) -> bytes: | ||
| return _sse( | ||
| { | ||
| "id": "chatcmpl-1", | ||
| "object": "chat.completion.chunk", | ||
| "created": 0, | ||
| "model": "google/gemma-4-31b-it", | ||
| "choices": [{"index": 0, "delta": delta, "finish_reason": None}], | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| # The gateway stamps its deployment and billing tier onto the leading delta, which | ||
| # carries no content of its own. | ||
| _METADATA_ONLY = _chunk( | ||
| { | ||
| "role": "assistant", | ||
| "extra_content": { | ||
| "livekit": {"inference_deployment": "d", "inference_tier_billed": "standard"} | ||
| }, | ||
| } | ||
| ) | ||
| _TEXT = _chunk({"role": "assistant", "content": "hello"}) | ||
| _TOOL_NAME = _chunk( | ||
| { | ||
| "role": "assistant", | ||
| "tool_calls": [ | ||
| {"index": 0, "id": "call_1", "type": "function", "function": {"name": "lookup"}} | ||
| ], | ||
| } | ||
| ) | ||
| _TOOL_ARGS = _chunk( | ||
| {"role": "assistant", "tool_calls": [{"index": 0, "function": {"arguments": '{"q":"x"}'}}]} | ||
| ) | ||
| _TOOL_DONE = _sse( | ||
| { | ||
| "id": "chatcmpl-1", | ||
| "object": "chat.completion.chunk", | ||
| "created": 0, | ||
| "model": "google/gemma-4-31b-it", | ||
| "choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}], | ||
| } | ||
| ) | ||
| _USAGE = _sse( | ||
| { | ||
| "id": "chatcmpl-1", | ||
| "object": "chat.completion.chunk", | ||
| "created": 0, | ||
| "model": "google/gemma-4-31b-it", | ||
| "choices": [], | ||
| "usage": {"prompt_tokens": 11, "completion_tokens": 7, "total_tokens": 18}, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| class _StallingStream(httpx.AsyncByteStream): | ||
| """Yields the given SSE bytes, then stalls out like a provider going quiet.""" | ||
|
|
||
| def __init__(self, chunks: list[bytes]) -> None: | ||
| self._chunks = chunks | ||
|
|
||
| async def __aiter__(self) -> AsyncIterator[bytes]: | ||
| for chunk in self._chunks: | ||
| yield chunk | ||
| raise httpx.ReadTimeout("stalled mid-stream") | ||
|
|
||
|
|
||
| class _CompletedStream(httpx.AsyncByteStream): | ||
| """Yields the given SSE bytes, then ends the stream cleanly.""" | ||
|
|
||
| def __init__(self, chunks: list[bytes]) -> None: | ||
| self._chunks = chunks | ||
|
|
||
| async def __aiter__(self) -> AsyncIterator[bytes]: | ||
| for chunk in self._chunks: | ||
| yield chunk | ||
| yield b"data: [DONE]\n\n" | ||
|
|
||
|
|
||
| def _llm_for( | ||
| responder: Callable[[int], httpx.AsyncByteStream], | ||
| ) -> tuple[LLM, list[httpx.Request], list[LLMMetrics]]: | ||
| attempts: list[httpx.Request] = [] | ||
| metrics: list[LLMMetrics] = [] | ||
|
|
||
| def handler(request: httpx.Request) -> httpx.Response: | ||
| attempts.append(request) | ||
| return httpx.Response( | ||
| 200, | ||
| headers={"content-type": "text/event-stream"}, | ||
| stream=responder(len(attempts)), | ||
| ) | ||
|
|
||
| # Long enough to keep PyJWT's short-key warning out of the suite output. | ||
| fake_secret = "f" * 32 | ||
| llm_model = LLM(model="google/gemma-4-31b-it", api_key=fake_secret, api_secret=fake_secret) | ||
| llm_model._client = openai.AsyncClient( | ||
| api_key=fake_secret, | ||
| base_url="http://inference.test/v1", | ||
| max_retries=0, | ||
| http_client=httpx.AsyncClient(transport=httpx.MockTransport(handler)), | ||
| ) | ||
| llm_model.on("metrics_collected", metrics.append) | ||
| return llm_model, attempts, metrics | ||
|
|
||
|
|
||
| def _drain(llm_model: LLM, *, max_retry: int): | ||
| chat_ctx = llm.ChatContext.empty() | ||
| chat_ctx.add_message(role="user", content="hi") | ||
| return llm_model.chat( | ||
| chat_ctx=chat_ctx, | ||
| conn_options=APIConnectOptions(max_retry=max_retry, retry_interval=0.0, timeout=5.0), | ||
| ) | ||
|
|
||
|
|
||
| async def _run(chunks: list[bytes], *, max_retry: int) -> tuple[Exception, list[httpx.Request]]: | ||
| llm_model, attempts, _ = _llm_for(lambda _: _StallingStream(chunks)) | ||
|
|
||
| with pytest.raises(Exception) as exc_info: # noqa: PT011 | ||
| async with _drain(llm_model, max_retry=max_retry) as stream: | ||
| async for _ in stream: | ||
| pass | ||
|
|
||
| return exc_info.value, attempts | ||
|
|
||
|
|
||
| async def _run_to_completion( | ||
| responder: Callable[[int], httpx.AsyncByteStream], *, max_retry: int | ||
| ) -> tuple[list[LLMMetrics], list[httpx.Request]]: | ||
| llm_model, attempts, metrics = _llm_for(responder) | ||
|
|
||
| # aclose() awaits the metrics monitor, so metrics are settled once the block exits | ||
| async with _drain(llm_model, max_retry=max_retry) as stream: | ||
| async for _ in stream: | ||
| pass | ||
|
|
||
| return metrics, attempts | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_metadata_only_chunk_stays_retryable() -> None: | ||
| error, attempts = await _run([_METADATA_ONLY], max_retry=2) | ||
|
|
||
| assert len(attempts) == 3, "a stall after metadata alone must exhaust the retries" | ||
| assert "after 3 attempts" in str(error) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_generated_text_is_not_retried() -> None: | ||
| error, attempts = await _run([_METADATA_ONLY, _TEXT], max_retry=2) | ||
|
|
||
| assert len(attempts) == 1, "text already sent to the caller must not be regenerated" | ||
| assert "after" not in str(error) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_completed_tool_call_is_not_retried() -> None: | ||
| error, attempts = await _run([_METADATA_ONLY, _TOOL_NAME, _TOOL_ARGS, _TOOL_DONE], max_retry=2) | ||
|
|
||
| assert len(attempts) == 1, "a tool call already sent to the caller must not be re-issued" | ||
| assert "after" not in str(error) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_partial_tool_arguments_stay_retryable() -> None: | ||
| _, attempts = await _run([_METADATA_ONLY, _TOOL_NAME, _TOOL_ARGS], max_retry=2) | ||
|
|
||
| assert len(attempts) == 3, "arguments still streaming have reached nobody" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_stream_read_timeout_is_a_timeout_error() -> None: | ||
| error, _ = await _run([_TEXT], max_retry=0) | ||
|
|
||
| assert isinstance(error, APITimeoutError), "a stalled stream body is a timeout, not a connect" | ||
| assert "timed out" in str(error).lower() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_ttft_spans_the_retry() -> None: | ||
| """The clock runs until generation, not until the failed attempt's metadata.""" | ||
|
|
||
| def responder(attempt: int) -> httpx.AsyncByteStream: | ||
| if attempt == 1: | ||
| return _StallingStream([_METADATA_ONLY]) | ||
| return _CompletedStream([_METADATA_ONLY, _TEXT, _USAGE]) | ||
|
|
||
| metrics, attempts = await _run_to_completion(responder, max_retry=2) | ||
|
|
||
| assert len(attempts) == 2 | ||
| assert len(metrics) == 1 | ||
| # the first retry always waits APIConnectOptions._interval_for_retry(0) == 0.1s | ||
| assert metrics[0].ttft > 0.09, "ttft latched on the failed attempt's metadata chunk" | ||
| assert metrics[0].ttft <= metrics[0].duration | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_token_counts_survive_a_response_that_generates_nothing() -> None: | ||
| """Metadata and usage but no output: ttft is unmeasurable, the tokens still are.""" | ||
| metrics, attempts = await _run_to_completion( | ||
| lambda _: _CompletedStream([_METADATA_ONLY, _USAGE]), max_retry=2 | ||
| ) | ||
|
|
||
| assert len(attempts) == 1 | ||
| assert len(metrics) == 1 | ||
| assert metrics[0].ttft == -1 | ||
| assert metrics[0].completion_tokens == 7 | ||
| assert metrics[0].total_tokens == 18 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe?