Skip to content

Commit 9c57a2b

Browse files
fix(agent): scope OPENAI_COMPAT_API parsing + add one-shot chat→responses auto-upgrade
Two follow-ups from review on #604. 1. Anthropic startup hardening (Max #1) `OPENAI_COMPAT_API` was parsed unconditionally, so a stray bad value in an Anthropic-only env broke startup. Parse it only inside the `Provider::OpenAi` arm of `Config::from_env`. Anthropic gets a placeholder `OpenAiApi::ChatCompletions` it never reads. New tests pin the parser behavior without touching process env. 2. One-shot chat→responses auto-upgrade (Max #2, Tyler "automatic detection/fallthrough") When `OPENAI_COMPAT_API=auto` and the provider replies to a Chat Completions request with a body that explicitly names `/v1/responses` (or the prose "use the Responses API"), latch a process-wide sticky-cached upgrade and re-issue the same request on `/v1/responses`. Subsequent calls skip the chat attempt entirely. Pinned values (`OPENAI_COMPAT_API=chat`|`responses`) never auto-upgrade. Signal matcher (`is_responses_required_error`) is intentionally narrow — only matches the literal path `/v1/responses` or specific prose phrases, so we don't get fooled by unrelated 4xx bodies. New `Config.openai_api_auto: bool` records whether the operator resolved-by-auto vs. pinned, so we know when to enable the upgrade. `Llm` gains an `AtomicBool` for the sticky upgrade, plus three small helpers (`effective_openai_api`, `should_try_auto_upgrade`, `latch_responses_upgrade`) so the dispatch reads straight through. Logged at WARN once per process: `"openai chat-completions endpoint reported that this model requires the Responses API; auto-upgrading subsequent OpenAI calls to /v1/responses for the rest of this process"`, with the provider error body attached. Tests: - 4 new unit tests for `is_responses_required_error` covering the Databricks GPT-5.5 signal, OpenAI prose phrasing, and explicit non-matches for `invalid_api_key`, generic `unsupported_parameter`, and empty body. - 3 new unit tests for `parse_openai_api` covering unset-defaults-to-auto, case-insensitive explicit values with whitespace, and rejected garbage. - New integration test `tests/openai_auto_upgrade.rs` spawns a fake provider that 400s on `/chat/completions` with the Databricks signal and 200s on `/responses`. Drives sprout-agent through ACP and asserts `stopReason=end_turn` plus chat-hit-once / responses-hit-once. 65 tests pass, 0 fail. clippy `-D warnings` clean. cargo fmt clean. Live smoke against api.openai.com with gpt-5-mini still 3/3 PASS. Signed-off-by: Tyler Longwell <[email protected]>
1 parent 8486536 commit 9c57a2b

3 files changed

Lines changed: 538 additions & 52 deletions

File tree

‎crates/sprout-agent/src/config.rs‎

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ pub struct Config {
7373
/// Anthropic`. Set via `OPENAI_COMPAT_API`; defaults to auto-select
7474
/// based on `base_url`.
7575
pub openai_api: OpenAiApi,
76+
/// `true` when `openai_api` was resolved by auto-detection (i.e.
77+
/// `OPENAI_COMPAT_API` was unset or `auto`). When `true` and the
78+
/// resolved value is `ChatCompletions`, `Llm` may upgrade to
79+
/// `Responses` once after observing a specific "use /v1/responses"
80+
/// error from the provider. `false` means the operator pinned the
81+
/// choice and the auto-upgrade path is disabled.
82+
pub openai_api_auto: bool,
7683
}
7784

7885
impl Config {
@@ -82,19 +89,43 @@ impl Config {
8289
"openai" | "openai-compat" => Provider::OpenAi,
8390
o => return Err(format!("config: SPROUT_AGENT_PROVIDER={o} not supported")),
8491
};
85-
let (api_key, model, base_url) = match provider {
92+
let (api_key, model, base_url, openai_api, openai_api_auto) = match provider {
8693
Provider::Anthropic => (
8794
req("ANTHROPIC_API_KEY")?,
8895
req("ANTHROPIC_MODEL")?,
8996
env_or("ANTHROPIC_BASE_URL", "https://api.anthropic.com"),
97+
// Unused for Anthropic. A placeholder value keeps `Config`
98+
// sized and avoids `Option`/`unwrap` plumbing in the hot
99+
// path. We intentionally do NOT parse `OPENAI_COMPAT_API`
100+
// here — a stray invalid value in an Anthropic-only env
101+
// must not break startup.
102+
OpenAiApi::ChatCompletions,
103+
false,
90104
),
91-
Provider::OpenAi => (
92-
req("OPENAI_COMPAT_API_KEY")?,
93-
req("OPENAI_COMPAT_MODEL")?,
94-
env_or("OPENAI_COMPAT_BASE_URL", "https://api.openai.com/v1"),
95-
),
105+
Provider::OpenAi => {
106+
let base_url = env_or("OPENAI_COMPAT_BASE_URL", "https://api.openai.com/v1");
107+
let raw = env("OPENAI_COMPAT_API");
108+
// "auto" (or unset/empty) means we may upgrade chat→responses
109+
// on a specific error from the provider. An explicit value
110+
// pins the choice.
111+
let openai_api_auto = matches!(
112+
raw.as_deref()
113+
.unwrap_or("auto")
114+
.trim()
115+
.to_ascii_lowercase()
116+
.as_str(),
117+
"auto" | ""
118+
);
119+
let openai_api = parse_openai_api(raw.as_deref(), &base_url)?;
120+
(
121+
req("OPENAI_COMPAT_API_KEY")?,
122+
req("OPENAI_COMPAT_MODEL")?,
123+
base_url,
124+
openai_api,
125+
openai_api_auto,
126+
)
127+
}
96128
};
97-
let openai_api = parse_openai_api_env(&base_url)?;
98129
let system_prompt = match (env("SPROUT_AGENT_SYSTEM_PROMPT"), env("SPROUT_AGENT_SYSTEM_PROMPT_FILE")) {
99130
(Some(_), Some(_)) => return Err(
100131
"config: SPROUT_AGENT_SYSTEM_PROMPT and SPROUT_AGENT_SYSTEM_PROMPT_FILE are mutually exclusive".into()),
@@ -110,6 +141,7 @@ impl Config {
110141
base_url,
111142
anthropic_api_version: env_or("ANTHROPIC_API_VERSION", "2023-06-01"),
112143
openai_api,
144+
openai_api_auto,
113145
max_rounds: parse_env("SPROUT_AGENT_MAX_ROUNDS", 0)?,
114146
max_output_tokens: parse_env("SPROUT_AGENT_MAX_OUTPUT_TOKENS", 32_768)?,
115147
llm_timeout: Duration::from_secs(parse_env("SPROUT_AGENT_LLM_TIMEOUT_SECS", 120)?),
@@ -203,9 +235,15 @@ fn req(k: &str) -> Result<String, String> {
203235
/// Parse `OPENAI_COMPAT_API` and, on `auto` (or unset), pick a default
204236
/// from `base_url`. Official OpenAI gets Responses; everything else gets
205237
/// Chat Completions.
206-
fn parse_openai_api_env(base_url: &str) -> Result<OpenAiApi, String> {
207-
let raw = env("OPENAI_COMPAT_API").unwrap_or_else(|| "auto".into());
208-
match raw.to_ascii_lowercase().as_str() {
238+
///
239+
/// Only called when `provider = OpenAi`. We deliberately don't read this
240+
/// env when `provider = Anthropic` so a stray invalid value cannot break
241+
/// an Anthropic-only deployment.
242+
///
243+
/// The pure parser takes `raw` as a parameter so tests can drive the
244+
/// full input matrix without touching process env.
245+
fn parse_openai_api(raw: Option<&str>, base_url: &str) -> Result<OpenAiApi, String> {
246+
match raw.unwrap_or("auto").trim().to_ascii_lowercase().as_str() {
209247
"chat" | "chat_completions" | "chat-completions" => Ok(OpenAiApi::ChatCompletions),
210248
"responses" => Ok(OpenAiApi::Responses),
211249
"auto" | "" => Ok(auto_openai_api(base_url)),
@@ -431,6 +469,45 @@ mod tests {
431469
}
432470
}
433471

472+
#[test]
473+
fn parse_openai_api_unset_defaults_to_auto() {
474+
assert_eq!(
475+
parse_openai_api(None, "https://api.openai.com/v1").unwrap(),
476+
OpenAiApi::Responses,
477+
);
478+
assert_eq!(
479+
parse_openai_api(None, "http://localhost:11434/v1").unwrap(),
480+
OpenAiApi::ChatCompletions,
481+
);
482+
}
483+
484+
#[test]
485+
fn parse_openai_api_accepts_explicit_values() {
486+
let url = "http://example.invalid";
487+
assert_eq!(
488+
parse_openai_api(Some("chat"), url).unwrap(),
489+
OpenAiApi::ChatCompletions
490+
);
491+
assert_eq!(
492+
parse_openai_api(Some("chat-completions"), url).unwrap(),
493+
OpenAiApi::ChatCompletions
494+
);
495+
assert_eq!(
496+
parse_openai_api(Some("RESPONSES"), url).unwrap(),
497+
OpenAiApi::Responses
498+
);
499+
assert_eq!(
500+
parse_openai_api(Some(" auto "), "https://api.openai.com").unwrap(),
501+
OpenAiApi::Responses
502+
);
503+
}
504+
505+
#[test]
506+
fn parse_openai_api_rejects_garbage() {
507+
let err = parse_openai_api(Some("nope"), "http://example.invalid").unwrap_err();
508+
assert!(err.contains("OPENAI_COMPAT_API=nope"));
509+
}
510+
434511
#[test]
435512
fn auto_openai_api_does_not_match_lookalike_hosts() {
436513
// Defense against host-suffix mistakes: api.openai.com.evil.com

0 commit comments

Comments
 (0)