Repository navigation
Feat/oai streaming retry - #24
Open
timotheegath wants to merge 3 commits into
Open
timotheegath wants to merge 3 commits into
timotheegath wants to merge 3 commits into
Conversation
…reasoning_content Port the generic behaviors out of Colosseum's _patch_terrarium.py monkey-patch into the pre-existing `openai` provider (OpenAIClient only): - SSE streaming on by default (constructor flag `stream`, overridable via the `openai` config block). generate_response calls responses.create(..., stream=True) and consumes the event stream inside the retry wrapper (mid-stream errors retry), returning the Response from the response.completed event so get_usage / process_tool_calls / output_text are unchanged. Endpoints rejecting streaming (400/404/405/422/501) fall back: one retry without stream, then a plain blocking call. With SSE the SDK timeout applies per-read between chunks (idle-time) instead of capping total generation time. - Retry settings now resolve as openai-block keys (max_retries, retry_initial_s, retry_max_s, retry_multiplier, retry_jitter) -> OPENAI_* env vars (built from the new env_prefix ctor arg, default "OPENAI") -> defaults (6, 0.5, 20, 2, 0.1). The hand-rolled retry wrapper (Retry-After, 408/409/429/5xx + connection/timeout) is unchanged. - Default request_timeout raised 30s -> 180s (config key request_timeout). - strip_reasoning_content (default True): request-only strip of reasoning items in _sanitize_response_input (now instance-aware); the agent context/trajectory keeps them for logging. - get_client_instance openai branch reads the openai config block and passes request_timeout/stream/strip_reasoning_content/the five retry keys through. - Subclasses (Foundry, Grok) keep the blocking transport via _stream_default/__init_subclass__, so their behavior is unchanged. - Docs: OPENAI_* retry env vars in .env.example; commented openai block keys in examples/configs/hospital.yaml. - Tests: fake SSE stream via response.completed, stream-reject -> blocking fallback ladder, mid-stream error retry, retry config precedence (block > env > defaults, env_prefix), and strip_reasoning_content filtering. Co-authored-by: timotheegath <timotheegath@users.noreply.github.com>
Endpoints like OpenRouter end the stream with response.incomplete when the output budget is exhausted instead of response.completed; returning the partial response instead of raising keeps truncated turns usable. A stream that closes with no terminal event, or that fails via response.failed/error with a transient code, is now retried with backoff; deterministic client-error codes still fail fast. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
feat/oai-streaming-retry
Improvements to general and OpenAI transport to reduce cutting off agents when using long reasoning, and stabilise experiments.
Terrarium's openai provider (OpenAIClient) now natively supports SSE streaming (default on, with blocking fallback), configurable retry-with-backoff (openai config block → OPENAI_* env vars → defaults), a 180s default request timeout, and request-only stripping of reasoning items.
Why this is important
SSE matters because with a blocking request the read timeout silently caps total generation time for long-thinking reasoning models, whereas over SSE it measures idle time between chunks. I observed durin runtime that agents were frequently cut short in their reasoning due to an API timeout.
Provider-specific retry config keeps backoff tunable per deployment; and stripping reasoning items from the request input (while keeping them in the trajectory for logging) avoids re-injecting chain-of-thought into subsequent prompts, making them lighter.
Implementation
terrarium/llm/clients/openai_client.py- OpenAIClient only:generate_responsecallsresponses.create(..., stream=True), consumes the event stream inside the retry wrapper (to have mid-stream errors retry), and returns theResponsefromresponse.completed.strip_reasoning_content=Truedefault, request-only strip of reasoning items in the now instance-aware_sanitize_response_input(trajectory keeps them).terrarium/utils.py— openai branch ofget_client_instancereads the openai block and passes request_timeout / stream / strip_reasoning_content / the five retry keys..env.example(OPENAI_* retry vars),examples/configs/hospital.yaml(commented openai block keys).