Skip to content

Feat/oai streaming retry - #24

Open
timotheegath wants to merge 3 commits into
umass-aisec:mainfrom
timotheegath:feat/oai-streaming-retry
Open

timotheegath wants to merge 3 commits into
umass-aisec:mainfrom
timotheegath:feat/oai-streaming-retry

Conversation

@timotheegath

Copy link
Copy Markdown
Contributor

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:

  • SSE streaming default ON generate_response calls responses.create(..., stream=True), consumes the event stream inside the retry wrapper (to have mid-stream errors retry), and returns the Response from response.completed.
  • Retry config resolution: ctor kwargs (fed from the openai block) → <env_prefix>_* env vars (new env_prefix="OPENAI" arg) → defaults (6, 0.5, 20, 2, 0.1).
  • timeout default 30s → 180s; strip_reasoning_content=True default, request-only strip of reasoning items in the now instance-aware _sanitize_response_input (trajectory keeps them).
  • terrarium/utils.py — openai branch of get_client_instance reads 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).

mistral-vibe and others added 3 commits November 25, 2026 11:26
…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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants