Fix/auth and model robustness#83
Merged
Merged
Conversation
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
force-pushed
the
fix/auth-and-model-robustness
branch
from
June 30, 2026 18:59
26879ee to
ed35a88
Compare
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>
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.
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 intoos.environviaset_env_auth(which usessetdefault) 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 bothagent.modelandagent.chat_history.model. The chat history keeps its own model reference (for token budgeting and the model metadata it serializes for UIs), so assigningagent.modelalone left it stale. Gives callers one correct way to swap the model at runtime.An explicitly-configured key now wins over an
OPENAI_API_KEY/AZURE_OPENAI_API_KEYalready 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.py—set_modelupdates both the agent and chat-history model.