Skip to content

fix(llm): strip trailing/leading whitespace from API keys across all …#5999

Open
aryxnn wants to merge 1 commit into
crewAIInc:mainfrom
aryxnn:fix-api-key-whitespace
Open

fix(llm): strip trailing/leading whitespace from API keys across all …#5999
aryxnn wants to merge 1 commit into
crewAIInc:mainfrom
aryxnn:fix-api-key-whitespace

Conversation

@aryxnn
Copy link
Copy Markdown

@aryxnn aryxnn commented Jun 1, 2026

Description
This PR resolves issue #5622 where valid API keys read from .env files or environment variables fail with 401 invalid_api_key errors due to trailing or leading whitespace, newlines, or tabs.

Changes
Automatically formats API keys by calling .strip() across all native LLM providers (OpenAI, Anthropic, Gemini, Azure, and OpenAI-compatible).

Summary by CodeRabbit

Bug Fixes

  • Improved API key handling across all LLM providers (Anthropic, Azure, Gemini, OpenAI, and OpenAI-compatible) by automatically trimming leading and trailing whitespace from API credentials, ensuring authentication works reliably even if keys contain accidental whitespace.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 1, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 19488cb1-ad9b-4e6d-91c0-e87c767bbcbc

📥 Commits

Reviewing files that changed from the base of the PR and between 4dafb05 and 746f009.

📒 Files selected for processing (5)
  • lib/crewai/src/crewai/llms/providers/anthropic/completion.py
  • lib/crewai/src/crewai/llms/providers/azure/completion.py
  • lib/crewai/src/crewai/llms/providers/gemini/completion.py
  • lib/crewai/src/crewai/llms/providers/openai/completion.py
  • lib/crewai/src/crewai/llms/providers/openai_compatible/completion.py

📝 Walkthrough

Walkthrough

This PR applies API key whitespace sanitization across all five LLM provider implementations in CrewAI. Each provider now trims leading and trailing whitespace from API keys at initialization and/or during deferred client setup to prevent credential validation failures from whitespace pollution.

Changes

API Key Whitespace Sanitization

Layer / File(s) Summary
API key whitespace trimming in all LLM providers
lib/crewai/src/crewai/llms/providers/anthropic/completion.py, lib/crewai/src/crewai/llms/providers/azure/completion.py, lib/crewai/src/crewai/llms/providers/gemini/completion.py, lib/crewai/src/crewai/llms/providers/openai/completion.py, lib/crewai/src/crewai/llms/providers/openai_compatible/completion.py
Anthropic sanitizes API keys in _get_client_params after environment variable loading. Azure strips whitespace in both field normalization and deferred client construction. Gemini trims keys during field normalization and sync client initialization. OpenAI applies sanitization in field normalization and lazy client-parameter construction. OpenAI Compatible trims whitespace in API key resolution for both explicit and environment-sourced keys.

🎯 2 (Simple) | ⏱️ ~8 minutes

🐰 Five providers, one trim,
Whitespace stripped to the brim!
API keys now pristine and clean,
The safest credentials ever seen. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding API key whitespace stripping across all LLM providers.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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