BaseModel Class — anthropic-sdk-python Architecture
Architecture documentation for the BaseModel class in _models.py from the anthropic-sdk-python codebase.
Entity Profile
Dependency Diagram
graph TD 17ce5647_6f06_0676_a4a5_e378a3f57cb1["BaseModel"] c2dd17c5_d7b6_a984_d7cd_eeb93072b3df["_ConfigProtocol"] 17ce5647_6f06_0676_a4a5_e378a3f57cb1 -->|extends| c2dd17c5_d7b6_a984_d7cd_eeb93072b3df 3912cc3f_b0e8_a732_b8e2_613b018b830d["_models.py"] 17ce5647_6f06_0676_a4a5_e378a3f57cb1 -->|defined in| 3912cc3f_b0e8_a732_b8e2_613b018b830d 95c7e945_50f2_bb18_2948_bb6584934562["to_dict()"] 17ce5647_6f06_0676_a4a5_e378a3f57cb1 -->|method| 95c7e945_50f2_bb18_2948_bb6584934562 6f82b0a7_6591_72e0_8921_bedf8e6504de["to_json()"] 17ce5647_6f06_0676_a4a5_e378a3f57cb1 -->|method| 6f82b0a7_6591_72e0_8921_bedf8e6504de d630bc66_31d2_42c7_013b_bd4ac560befb["__str__()"] 17ce5647_6f06_0676_a4a5_e378a3f57cb1 -->|method| d630bc66_31d2_42c7_013b_bd4ac560befb 57e8da7b_af31_35a8_78f7_c274f7fbb350["construct()"] 17ce5647_6f06_0676_a4a5_e378a3f57cb1 -->|method| 57e8da7b_af31_35a8_78f7_c274f7fbb350
Relationship Graph
Source Code
src/anthropic/_models.py lines 97–412
class BaseModel(pydantic.BaseModel):
if PYDANTIC_V1:
@property
@override
def model_fields_set(self) -> set[str]:
# a forwards-compat shim for pydantic v2
return self.__fields_set__ # type: ignore
class Config(pydantic.BaseConfig): # pyright: ignore[reportDeprecated]
extra: Any = pydantic.Extra.allow # type: ignore
else:
model_config: ClassVar[ConfigDict] = ConfigDict(
extra="allow", defer_build=coerce_boolean(os.environ.get("DEFER_PYDANTIC_BUILD", "true"))
)
if TYPE_CHECKING:
_request_id: Optional[str] = None
"""The ID of the request, returned via the `request-id` header. Useful for debugging requests and reporting issues to Anthropic.
This will **only** be set for the top-level response object, it will not be defined for nested objects. For example:
```py
message = await client.messages.create(...)
message._request_id # req_xxx
message.usage._request_id # raises `AttributeError`
```
Note: unlike other properties that use an `_` prefix, this property
*is* public. Unless documented otherwise, all other `_` prefix properties,
methods and modules are *private*.
"""
def to_dict(
self,
*,
mode: Literal["json", "python"] = "python",
use_api_names: bool = True,
exclude_unset: bool = True,
exclude_defaults: bool = False,
exclude_none: bool = False,
warnings: bool = True,
) -> dict[str, object]:
"""Recursively generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
By default, fields that were not set by the API will not be included,
and keys will match the API response, *not* the property names from the model.
For example, if the API responds with `"fooBar": true` but we've defined a `foo_bar: bool` property,
the output will use the `"fooBar"` key (unless `use_api_names=False` is passed).
Args:
mode:
If mode is 'json', the dictionary will only contain JSON serializable types. e.g. `datetime` will be turned into a string, `"2024-3-22T18:11:19.117000Z"`.
If mode is 'python', the dictionary may contain any Python objects. e.g. `datetime(2024, 3, 22)`
use_api_names: Whether to use the key that the API responded with or the property name. Defaults to `True`.
exclude_unset: Whether to exclude fields that have not been explicitly set.
exclude_defaults: Whether to exclude fields that are set to their default value from the output.
exclude_none: Whether to exclude fields that have a value of `None` from the output.
warnings: Whether to log warnings when invalid fields are encountered. This is only supported in Pydantic v2.
"""
return self.model_dump(
mode=mode,
by_alias=use_api_names,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
warnings=warnings,
)
def to_json(
self,
*,
indent: int | None = 2,
use_api_names: bool = True,
exclude_unset: bool = True,
exclude_defaults: bool = False,
exclude_none: bool = False,
warnings: bool = True,
) -> str:
"""Generates a JSON string representing this model as it would be received from or sent to the API (but with indentation).
Domain
Defined In
Extends
Source
Frequently Asked Questions
What is the BaseModel class?
BaseModel is a class in the anthropic-sdk-python codebase, defined in src/anthropic/_models.py.
Where is BaseModel defined?
BaseModel is defined in src/anthropic/_models.py at line 97.
What does BaseModel extend?
BaseModel extends _ConfigProtocol.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free