microsoft/semantic-kernel · critical · ServiceInitializationError
Failed to create Anthropic settings.
Error message
Failed to create Anthropic settings.
What it means
During AnthropicChatCompletion.__init__, AnthropicSettings is constructed from the api_key, model id, env file, and environment. If pydantic validation fails (most commonly a missing or malformed API key), the ValidationError is wrapped as ServiceInitializationError and chained as __cause__.
Source
Thrown at python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py:110
ai_model_id: Anthropic model name, see
https://docs.anthropic.com/en/docs/about-claude/models#model-names
service_id: Service ID tied to the execution settings.
api_key: The optional API key to use. If provided will override,
the env vars or .env file value.
async_client: An existing client to use.
env_file_path: Use the environment settings file as a fallback
to environment variables.
env_file_encoding: The encoding of the environment settings file.
"""
try:
anthropic_settings = AnthropicSettings(
api_key=api_key,
chat_model_id=ai_model_id,
env_file_path=env_file_path,
env_file_encoding=env_file_encoding,
)
except ValidationError as ex:
raise ServiceInitializationError("Failed to create Anthropic settings.", ex) from ex
if not anthropic_settings.chat_model_id:
raise ServiceInitializationError("The Anthropic chat model ID is required.")
if not async_client:
async_client = AsyncAnthropic(
api_key=anthropic_settings.api_key.get_secret_value(),
)
super().__init__(
async_client=async_client,
service_id=service_id or anthropic_settings.chat_model_id,
ai_model_id=anthropic_settings.chat_model_id,
)
# region Overriding base class methods
# Override from AIServiceClientBaseView on GitHub (pinned to c028a0c7dc)
Solutions
- Set ANTHROPIC_API_KEY in the environment or .env, or pass api_key explicitly to the constructor.
- Inspect the chained ValidationError (`e.__cause__`) to see which field(s) failed.
- Verify env_file_path and env_file_encoding point to a readable file if you rely on .env fallback.
Example fix
// before
svc = AnthropicChatCompletion() # no ANTHROPIC_API_KEY anywhere -> ValidationError wrapped
// after
svc = AnthropicChatCompletion(
ai_model_id="claude-3-5-sonnet-latest",
api_key=os.environ["ANTHROPIC_API_KEY"],
) Defensive patterns
Strategy: validation
Validate before calling
import os
key = os.environ.get('ANTHROPIC_API_KEY') or api_key
if not key or len(key) < 20:
raise ValueError('ANTHROPIC_API_KEY missing or malformed') Type guard
def has_valid_anthropic_key() -> bool:
key = os.environ.get('ANTHROPIC_API_KEY')
return bool(key) and len(key) >= 20 Try / catch
from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError
try:
svc = AnthropicChatCompletion(ai_model_id=model, api_key=key)
except ServiceInitializationError as e:
cause = e.__cause__ # pydantic ValidationError with field details
raise SystemExit(f'Anthropic init failed: {cause}') Prevention
- Set ANTHROPIC_API_KEY in the environment or .env
- Pass api_key explicitly in tests
- Verify env_file_path/encoding when relying on .env
- Inspect the chained ValidationError for the failing field
When it happens
Trigger: Constructing AnthropicChatCompletion without a valid ANTHROPIC_API_KEY (not in env, not in .env, not passed), with a malformed key, or with an unreadable/mis-encoded env file.
Common situations: No ANTHROPIC_API_KEY in the environment; wrong env var name; .env file path or encoding mismatch; key passed as an empty string; secrets not loaded in the deployment.
Related errors
- The Anthropic chat model ID is required.
- Failed to create Google settings.
- Failed to create MongoDB Atlas settings.
- Failed to create Azure OpenAI settings: {exc}
- Please provide an Azure OpenAI endpoint
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/3aebba996d9bc0da.
Report an issue: GitHub.