Skip to content

Prefer AICHAT_API_KEY in default lookup#56

Merged
raghubetina merged 1 commit intomainfrom
rb-aichat-api-key
Mar 2, 2026
Merged

Prefer AICHAT_API_KEY in default lookup#56
raghubetina merged 1 commit intomainfrom
rb-aichat-api-key

Conversation

@raghubetina
Copy link
Contributor

Summary

This PR updates default API key lookup to prefer AICHAT_API_KEY while
preserving backward compatibility with OPENAI_API_KEY.

What Changed

  • Updated AI::Chat.new and AI::Chat.generate_schema! default key
    resolution:
    • first tries AICHAT_API_KEY
    • falls back to OPENAI_API_KEY when AICHAT_API_KEY is missing or
      empty
  • Preserved explicit override semantics:
    • api_key: still has highest precedence
    • api_key_env_var: still uses that exact environment variable
  • Added/updated tests for:
    • default env var precedence
    • empty AICHAT_API_KEY fallback
    • explicit api_key_env_var: behavior
    • explicit api_key: behavior
  • Updated integration skip logic so integration tests run when either
    AICHAT_API_KEY or OPENAI_API_KEY is set
  • Updated README API key docs and direct OpenAI::Client example to match
    runtime behavior
  • Bumped gem version from 0.5.6 to 0.5.7
  • Added changelog entry for 0.5.7

Compatibility

  • Existing setups using only OPENAI_API_KEY continue to work unchanged.
  • Projects can now use a gem-scoped key via AICHAT_API_KEY without
    changing app-wide key names.

Verification

  • bundle exec standardrb lib/ai/chat.rb spec/unit/chat_spec.rb spec/integration/ai_chat_integration_spec.rb spec/support/integration_helper.rb
  • bundle exec rspec spec/unit/chat_spec.rb
  • bundle exec rspec spec/integration/ai_chat_integration_spec.rb:227

All commands passed.

Change default key resolution in AI::Chat and generate_schema!

to check AICHAT_API_KEY first and fall back to OPENAI_API_KEY

when the first value is missing or empty.

Keep explicit configuration behavior unchanged:

- api_key: still has highest precedence.

- api_key_env_var: still targets exactly one env var.

Add unit coverage for precedence and fallback rules, update

integration test setup to accept either env var, and refresh

README examples to match runtime behavior.

Bump gem version to 0.5.7 and add a changelog entry.
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6cfda555f5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".


it "accepts a custom environment variable name" do
ENV["CUSTOM_OPENAI_KEY"] = ENV["OPENAI_API_KEY"]
ENV["CUSTOM_OPENAI_KEY"] = ENV["AICHAT_API_KEY"] || ENV["OPENAI_API_KEY"]

Choose a reason for hiding this comment

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

P2 Badge Treat empty AICHAT_API_KEY as missing in integration key setup

Using ENV["AICHAT_API_KEY"] || ENV["OPENAI_API_KEY"] does not implement the intended fallback when AICHAT_API_KEY is present but empty, because Ruby treats "" as truthy. In environments where AICHAT_API_KEY="" and OPENAI_API_KEY is valid, this assigns an empty key to CUSTOM_OPENAI_KEY, so the custom-env integration test uses an invalid credential and can fail even though the runtime default lookup is designed to fall back to OPENAI_API_KEY for empty values.

Useful? React with 👍 / 👎.

@raghubetina raghubetina merged commit 6cfda55 into main Mar 2, 2026
2 checks passed
raghubetina added a commit that referenced this pull request Mar 2, 2026
In integration API key tests,

treats empty strings as present and skips fallback.

Use explicit empty checks before falling back to OPENAI_API_KEY

for custom env-var and direct api_key test setup.

This aligns test setup with runtime behavior and addresses the

PR #56 review finding.
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.

1 participant