Skip to content

Fix/auth and model robustness#83

Merged
mattprintz merged 2 commits into
mainfrom
fix/auth-and-model-robustness
Jul 1, 2026
Merged

Fix/auth and model robustness#83
mattprintz merged 2 commits into
mainfrom
fix/auth-and-model-robustness

Conversation

@brandomr

Copy link
Copy Markdown
Collaborator

Summary

Two related fixes to LLM model auth / lifecycle.

1. Explicit API key now takes precedence over the environment (openai/azure). OpenAIModel/AzureOpenAIModel.auth() wrote the key into os.environ via set_env_auth (which uses setdefault) and then read it back, so a value left in the environment by an earlier model instance shadowed a freshly-configured key — the new key only took effect after a process restart. An explicitly-configured key now overwrites the environment and wins; the environment is only consulted when no key is configured.

2. Agent.set_model() — a single method that updates both agent.model and agent.chat_history.model. The chat history keeps its own model reference (for token budgeting and the model metadata it serializes for UIs), so assigning
agent.model alone left it stale. Gives callers one correct way to swap the model at runtime.

⚠️ Behavior change

An explicitly-configured key now wins over an OPENAI_API_KEY / AZURE_OPENAI_API_KEY already set in the environment (previously the environment always won). The env var is still used when no key is configured, preserving the "set the key in your shell" workflow.

Scope

Only openai/azure used the env round-trip. anthropic, groq, gemini, bedrock and ollama bind the configured key directly and were unaffected (verified).

Tests

  • tests/test_auth_env_precedence.py — explicit key beats a stale env var; defers to env when no key configured.
  • tests/test_agent_set_model.pyset_model updates both the agent and chat-history model.

brandomr and others added 2 commits June 30, 2026 13:58
OpenAIModel/AzureOpenAIModel wrote the key into os.environ via set_env_auth
(setdefault) and then read it back, so a value left in the environment by an
earlier model instance shadowed a freshly-configured key: the new key only took
effect after a process restart.

Now an explicitly-configured key overwrites the environment and wins; the
environment is only consulted when no key is configured, preserving the
"user sets OPENAI_API_KEY in their shell" workflow.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…sync

The chat history keeps its own model reference (used for token budgeting and the
model metadata it serializes for UIs), so assigning `agent.model` alone leaves
that reference stale. set_model() updates both, giving callers a single correct
way to change the model at runtime instead of having to remember the coupling.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@brandomr
brandomr force-pushed the fix/auth-and-model-robustness branch from 26879ee to ed35a88 Compare June 30, 2026 18:59

@mattprintz mattprintz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

@mattprintz
mattprintz merged commit 98502ca into main Jul 1, 2026
1 check passed
@mattprintz
mattprintz deleted the fix/auth-and-model-robustness branch July 1, 2026 18:12
mattprintz pushed a commit that referenced this pull request Jul 1, 2026
* fix(openai/azure): let an explicit API key override a stale env value

OpenAIModel/AzureOpenAIModel wrote the key into os.environ via set_env_auth
(setdefault) and then read it back, so a value left in the environment by an
earlier model instance shadowed a freshly-configured key: the new key only took
effect after a process restart.

Now an explicitly-configured key overwrites the environment and wins; the
environment is only consulted when no key is configured, preserving the
"user sets OPENAI_API_KEY in their shell" workflow.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* feat(agent): add Agent.set_model() to keep model and chat history in sync

The chat history keeps its own model reference (used for token budgeting and the
model metadata it serializes for UIs), so assigning `agent.model` alone leaves
that reference stale. set_model() updates both, giving callers a single correct
way to change the model at runtime instead of having to remember the coupling.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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