{"record":{"id":"3aebba996d9bc0da","repo":"microsoft/semantic-kernel","slug":"failed-to-create-anthropic-settings","errorCode":null,"errorMessage":"Failed to create Anthropic settings.","messagePattern":"Failed to create Anthropic settings\\.","errorType":"exception","errorClass":"ServiceInitializationError","httpStatus":null,"severity":"critical","filePath":"python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py","lineNumber":110,"sourceCode":"            ai_model_id: Anthropic model name, see\n                https://docs.anthropic.com/en/docs/about-claude/models#model-names\n            service_id: Service ID tied to the execution settings.\n            api_key: The optional API key to use. If provided will override,\n                the env vars or .env file value.\n            async_client: An existing client to use.\n            env_file_path: Use the environment settings file as a fallback\n                to environment variables.\n            env_file_encoding: The encoding of the environment settings file.\n        \"\"\"\n        try:\n            anthropic_settings = AnthropicSettings(\n                api_key=api_key,\n                chat_model_id=ai_model_id,\n                env_file_path=env_file_path,\n                env_file_encoding=env_file_encoding,\n            )\n        except ValidationError as ex:\n            raise ServiceInitializationError(\"Failed to create Anthropic settings.\", ex) from ex\n\n        if not anthropic_settings.chat_model_id:\n            raise ServiceInitializationError(\"The Anthropic chat model ID is required.\")\n\n        if not async_client:\n            async_client = AsyncAnthropic(\n                api_key=anthropic_settings.api_key.get_secret_value(),\n            )\n\n        super().__init__(\n            async_client=async_client,\n            service_id=service_id or anthropic_settings.chat_model_id,\n            ai_model_id=anthropic_settings.chat_model_id,\n        )\n\n    # region Overriding base class methods\n\n    # Override from AIServiceClientBase","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py#L92-L128","documentation":"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__.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nsvc = AnthropicChatCompletion()  # no ANTHROPIC_API_KEY anywhere -> ValidationError wrapped\n\n// after\nsvc = AnthropicChatCompletion(\n    ai_model_id=\"claude-3-5-sonnet-latest\",\n    api_key=os.environ[\"ANTHROPIC_API_KEY\"],\n)","handlingStrategy":"validation","validationCode":"import os\nkey = os.environ.get('ANTHROPIC_API_KEY') or api_key\nif not key or len(key) < 20:\n    raise ValueError('ANTHROPIC_API_KEY missing or malformed')","typeGuard":"def has_valid_anthropic_key() -> bool:\n    key = os.environ.get('ANTHROPIC_API_KEY')\n    return bool(key) and len(key) >= 20","tryCatchPattern":"from semantic_kernel.exceptions.service_exceptions import ServiceInitializationError\ntry:\n    svc = AnthropicChatCompletion(ai_model_id=model, api_key=key)\nexcept ServiceInitializationError as e:\n    cause = e.__cause__  # pydantic ValidationError with field details\n    raise SystemExit(f'Anthropic init failed: {cause}')","preventionTips":["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"],"tags":["anthropic","config","secrets","initialization"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}