Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Anthropics Anthropic sdk python Package Init

From Leeroopedia
Knowledge Sources
Domains API_Client, SDK_Architecture
Last Updated 2026-02-15 12:00 GMT

Overview

The package __init__.py is the main entry point for the anthropic Python SDK, defining the public API surface by re-exporting all classes, exceptions, constants, and utilities that consumers of the library interact with.

Description

This module serves as the namespace aggregator for the entire Anthropic SDK. It imports and re-exports symbols from internal submodules so that users can access all public functionality through the top-level anthropic package rather than reaching into private internal modules.

The module pulls together several categories of exports: the primary client classes (Anthropic, AsyncAnthropic), streaming wrappers (Stream, AsyncStream), the complete exception hierarchy, type aliases and sentinel values (NotGiven, NOT_GIVEN, Omit), configuration constants (DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES), and HTTP client defaults. It also imports cloud provider integrations from lib.vertex, lib.bedrock, and lib.foundry via wildcard imports.

A post-import loop rewrites the __module__ attribute of all exported symbols so that error messages and introspection display anthropic.NotFoundError instead of anthropic._exceptions.NotFoundError, providing a cleaner developer experience. The module also calls _setup_logging() on import to configure the SDK logging subsystem.

Usage

This file is implicitly invoked whenever a user writes import anthropic or from anthropic import .... Developers should always import from the top-level anthropic namespace rather than from private submodules.

Code Reference

Source Location

Signature

The module does not define callable signatures itself. It re-exports the following public symbols via __all__:

__all__ = [
    "types",
    "__version__",
    "__title__",
    "NoneType",
    "Transport",
    "ProxiesTypes",
    "NotGiven",
    "NOT_GIVEN",
    "not_given",
    "Omit",
    "omit",
    "AnthropicError",
    "APIError",
    "APIStatusError",
    "APITimeoutError",
    "APIConnectionError",
    "APIResponseValidationError",
    "BadRequestError",
    "AuthenticationError",
    "PermissionDeniedError",
    "NotFoundError",
    "ConflictError",
    "UnprocessableEntityError",
    "RateLimitError",
    "InternalServerError",
    "Timeout",
    "RequestOptions",
    "Client",
    "AsyncClient",
    "Stream",
    "AsyncStream",
    "Anthropic",
    "AsyncAnthropic",
    "file_from_path",
    "BaseModel",
    "DEFAULT_TIMEOUT",
    "DEFAULT_MAX_RETRIES",
    "DEFAULT_CONNECTION_LIMITS",
    "DefaultHttpxClient",
    "DefaultAsyncHttpxClient",
    "DefaultAioHttpClient",
    "HUMAN_PROMPT",
    "AI_PROMPT",
    "beta_tool",
    "beta_async_tool",
    "transform_schema",
]

Import

import anthropic

# Or import specific symbols:
from anthropic import Anthropic, AsyncAnthropic
from anthropic import APIError, RateLimitError, AuthenticationError
from anthropic import NotGiven, NOT_GIVEN, Omit, omit
from anthropic import Stream, AsyncStream
from anthropic import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES

I/O Contract

Exported Categories

Category Symbols Source Module
Client Classes Anthropic, AsyncAnthropic, Client, AsyncClient _client
Streaming Stream, AsyncStream _client
Exceptions AnthropicError, APIError, APIStatusError, APITimeoutError, APIConnectionError, APIResponseValidationError, BadRequestError, AuthenticationError, PermissionDeniedError, NotFoundError, ConflictError, UnprocessableEntityError, RateLimitError, InternalServerError _exceptions
Type Sentinels NotGiven, NOT_GIVEN, not_given, Omit, omit, NoneType _types
Type Aliases Transport, ProxiesTypes, RequestOptions, Timeout _types, _client
Constants AI_PROMPT, HUMAN_PROMPT, DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES, DEFAULT_CONNECTION_LIMITS _constants
HTTP Clients DefaultHttpxClient, DefaultAsyncHttpxClient, DefaultAioHttpClient _base_client
Response APIResponse, AsyncAPIResponse _response
Models BaseModel _models
Utilities file_from_path, beta_tool, beta_async_tool, transform_schema _utils, lib.tools, lib._parse._transform
Cloud Providers AnthropicVertex, AnthropicBedrock, AnthropicFoundry, etc. (via wildcard imports) lib.vertex, lib.bedrock, lib.foundry

Module Initialization Side Effects

The module performs two side effects on import:

  1. Logging setup: _setup_logging() is called to configure the SDK's logging subsystem.
  2. Module rewriting: A loop over __all__ rewrites each symbol's __module__ to "anthropic" so that introspection and error messages reference the public namespace.
_setup_logging()

__locals = locals()
for __name in __all__:
    if not __name.startswith("__"):
        try:
            __locals[__name].__module__ = "anthropic"
        except (TypeError, AttributeError):
            pass

Usage Examples

# Standard usage: import from top-level namespace
import anthropic

client = anthropic.Anthropic()  # Uses ANTHROPIC_API_KEY env var
response = client.messages.create(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
)

# Access version info
print(anthropic.__version__)

# Access type system
from anthropic import NotGiven, NOT_GIVEN

def my_function(param: str | NotGiven = NOT_GIVEN):
    if isinstance(param, NotGiven):
        print("Parameter was not provided")

# Exception handling with top-level imports
from anthropic import Anthropic, RateLimitError, AuthenticationError

client = Anthropic()
try:
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello"}],
    )
except AuthenticationError:
    print("Invalid API key")
except RateLimitError:
    print("Rate limited, back off and retry")

Related Pages

Implements Principle

Related Implementations

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment