Implementation:Cohere ai Cohere python Types Init
| Knowledge Sources | |
|---|---|
| Domains | SDK, API_Client |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Types_Init is the package initialization module for cohere.types that exports all type definitions using lazy loading via __getattr__ for improved import performance.
Description
This module serves as the central export point for all Cohere SDK type definitions. Rather than eagerly importing all types at module load time (which would incur significant startup cost given the ~230 exported types), it employs a lazy loading strategy:
- A _dynamic_imports dictionary maps each type name (e.g.,
"ChatMessage") to its relative module path (e.g.,".chat_message"). - The __getattr__ function intercepts attribute access and dynamically imports the requested type from the correct submodule using importlib.import_module.
- A typing.TYPE_CHECKING block provides static type hints for all exports, enabling IDE autocompletion and type checker support without triggering actual imports at runtime.
- The __all__ list declares all 230+ exported type names for from cohere.types import * support.
- The __dir__ function returns the sorted list of lazy-loadable attribute names for introspection.
The types cover the full Cohere API surface including:
- Chat types -- ChatMessage, ChatMessageV2, ChatCitation, ChatStreamEvent, ChatDocument, ChatFinishReason, and many streaming event types (ChatContentDeltaEvent, ChatToolCallStartEvent, etc.)
- Embedding types -- EmbedResponse, EmbedByTypeResponse, EmbedInputType, EmbeddingType, EmbedJob, etc.
- Tool types -- Tool, ToolCall, ToolCallV2, ToolV2, ToolContent, ToolResult, ToolParameterDefinitionsValue
- Generation types -- Generation, SingleGeneration, GenerateStreamedResponse and related streaming types
- Classification types -- ClassifyResponse, ClassifyExample, ClassifyDataMetrics
- Reranking types -- RerankResponse, RerankDocument, RerankResponseResultsItem
- Model/Connector types -- GetModelResponse, ListModelsResponse, Connector, ConnectorOAuth
- Response format types -- ResponseFormat, ResponseFormatV2, JsonResponseFormat, CitationOptions
- Metadata types -- ApiMeta, Usage, UsageBilledUnits, UsageTokens
Usage
Use this module to import any Cohere SDK type definition. All types are accessible via from cohere.types import TypeName. The lazy loading ensures that only the types you actually reference are loaded into memory.
Code Reference
Source Location
- Repository: Cohere Python SDK
- File:
src/cohere/types/__init__.py
Signature
_dynamic_imports: typing.Dict[str, str] = {
"ApiMeta": ".api_meta",
"ChatMessage": ".chat_message",
"Tool": ".tool",
"ToolCall": ".tool_call",
# ... 230+ type mappings
}
def __getattr__(attr_name: str) -> typing.Any:
"""Lazily imports and returns the requested type from its submodule."""
...
def __dir__() -> list:
"""Returns sorted list of all lazily-loadable type names."""
...
__all__: list # 230+ exported type names
Import
# Import specific types (recommended)
from cohere.types import ChatMessage, Tool, ToolCall, GetModelResponse
# Import all types (uses __all__)
from cohere.types import *
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| attr_name (__getattr__) | str |
Yes | The name of the type to import (e.g., "ChatMessage", "Tool", "GetModelResponse"). |
Outputs
| Name | Type | Description |
|---|---|---|
| __getattr__ return | Any |
The requested type class, lazily imported from its submodule. |
| __dir__ return | list |
Sorted list of all available type names (230+ entries). |
| __all__ | list |
Complete list of all exported type names for wildcard imports. |
Usage Examples
# Import specific types for chat interactions
from cohere.types import ChatMessage, NonStreamedChatResponse, ChatStreamEvent
# Import tool-related types
from cohere.types import Tool, ToolCall, ToolCallV2, ToolV2, ToolParameterDefinitionsValue
# Import embedding types
from cohere.types import EmbedResponse, EmbedInputType, EmbeddingType
# Import model information types
from cohere.types import GetModelResponse, ListModelsResponse, CompatibleEndpoint
# Import response format types
from cohere.types import ResponseFormatV2, JsonResponseFormatV2, CitationOptions
# Use types for type hints in application code
from cohere.types import ChatMessageV2, AssistantChatMessageV2
def process_messages(messages: list[ChatMessageV2]) -> None:
for msg in messages:
if isinstance(msg, AssistantChatMessageV2):
print(msg)
# Discover available types
import cohere.types
print(dir(cohere.types)) # Lists all 230+ available type names