Implementation:Cohere ai Cohere python UncheckedBaseModel
| Knowledge Sources | |
|---|---|
| Domains | SDK, API_Client |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
UncheckedBaseModel provides unvalidated model construction for efficient object creation, bypassing Pydantic validation during deserialization.
Description
The UncheckedBaseModel class extends UniversalBaseModel (which itself extends pydantic.BaseModel) to provide a fast-path construction mechanism that skips Pydantic's standard field validation. It overrides both model_construct (Pydantic v2) and construct (Pydantic v1) methods to build model instances directly by setting __dict__ via object.__setattr__ rather than going through the normal Pydantic initialization pipeline.
The module also includes the standalone function construct_type, which recursively coerces raw Python objects (dicts, lists, sets, primitives, dates, UUIDs) into their target typed representations without full validation. This is used throughout the SDK for efficient response deserialization.
Key supporting components in this module include:
- UnionMetadata -- A helper class that stores a discriminant field name, used to resolve discriminated union types during construction.
- construct_type -- A recursive type coercion function that converts raw objects into typed Python objects (Pydantic models, lists, dicts, sets, datetimes, UUIDs, booleans, integers, and union types).
- _convert_union_type / _convert_undiscriminated_union_type -- Internal functions that handle resolution of both discriminated and undiscriminated union types by attempting to match against inner type variants.
The class is configured with extra="allow" so that unknown fields in API responses are preserved rather than discarded.
Usage
Use UncheckedBaseModel as the base class for SDK response types where performance during deserialization matters more than strict input validation. It is used internally by the Fern-generated SDK code -- SDK consumers typically do not subclass it directly.
Code Reference
Source Location
- Repository: Cohere Python SDK
- File:
src/cohere/core/unchecked_base_model.py
Signature
class UncheckedBaseModel(UniversalBaseModel):
model_config: typing.ClassVar[pydantic.ConfigDict] = pydantic.ConfigDict(extra="allow")
@classmethod
def model_construct(
cls: typing.Type["Model"],
_fields_set: typing.Optional[typing.Set[str]] = None,
**values: typing.Any,
) -> "Model": ...
@classmethod
def construct(
cls: typing.Type["Model"],
_fields_set: typing.Optional[typing.Set[str]] = None,
**values: typing.Any,
) -> "Model": ...
class UnionMetadata:
discriminant: str
def __init__(self, *, discriminant: str) -> None: ...
def construct_type(*, type_: typing.Type[typing.Any], object_: typing.Any) -> typing.Any: ...
Import
from cohere.core.unchecked_base_model import UncheckedBaseModel, construct_type, UnionMetadata
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| _fields_set | Optional[Set[str]] |
No | Set of field names that have been explicitly set. If None, defaults to all provided keys. |
| **values | Any |
No | Keyword arguments representing field names and their values to populate the model. |
| type_ (construct_type) | Type[Any] |
Yes | The target type to coerce the object into. |
| object_ (construct_type) | Any |
Yes | The raw object (dict, list, primitive) to be coerced into the target type. |
Outputs
| Name | Type | Description |
|---|---|---|
| (model_construct return) | Model |
An instance of the model class constructed without validation. |
| (construct return) | Model |
An instance of the model class constructed without validation. |
| (construct_type return) | Any |
The input object coerced to the target type, or the original object if coercion is not applicable. |
Usage Examples
# Internal SDK usage: constructing a response model without validation
from cohere.core.unchecked_base_model import UncheckedBaseModel, construct_type
from cohere.types.get_model_response import GetModelResponse
# Fast model construction from a raw API response dict
raw_response = {
"name": "command-a-03-2025",
"endpoints": ["chat"],
"finetuned": False,
"context_length": 256000.0,
"tokenizer_url": "https://storage.googleapis.com/cohere-public/tokenizers/command-a.json",
}
model_instance = construct_type(
type_=GetModelResponse,
object_=raw_response,
)
# Direct construct usage on a subclass
class MyModel(UncheckedBaseModel):
name: str
value: int
instance = MyModel.construct(name="test", value=42)
print(instance.name) # "test"
print(instance.value) # 42