Overview
The Google Gen AI SDK supports both Pydantic models and dictionaries for specifying parameters. This flexibility allows you to choose between type-safe Pydantic models or simple Python dictionaries based on your preferences.Importing Types
All types are available in thetypes module:
Using Pydantic Models vs Dictionaries
The SDK accepts both Pydantic model instances and plain dictionaries for most parameters.Using Pydantic Models
Pydantic models provide type safety and validation:Using Dictionaries
Dictionaries provide a more concise syntax:Both approaches produce the same result. The SDK automatically converts dictionaries
to the appropriate Pydantic models internally.
Common Types
Part
Represents a single piece of content (text, image, file, function call, etc.):Content
Represents a message with a role and multiple parts:GenerateContentConfig
Configuration for content generation:HttpOptions
Customize HTTP behavior for requests:TypedDict Variants
For each Pydantic model, there’s a correspondingTypedDict variant with a Dict suffix:
types.Content→types.ContentDicttypes.Part→types.PartDicttypes.GenerateContentConfig→types.GenerateContentConfigDicttypes.HttpOptions→types.HttpOptionsDict
Schema Types
JSONSchema
Define response schemas using JSON Schema format:Pydantic Model Schema
Use Pydantic models directly for schema definition:Enum Types
The SDK provides numerous enum types for various settings:HarmCategory
FunctionCallingConfigMode
FinishReason
The response includes a finish reason enum:File and Blob Types
FileData
References files stored in Google Cloud Storage:Blob
Contains inline binary data:Function Calling Types
FunctionDeclaration
Manually declare functions for the model to call:FunctionCall and FunctionResponse
Cache Types
CreateCachedContentConfig
Best Practices
Use Pydantic models for:
- Large, complex configurations
- When you want IDE autocomplete and type checking
- When building reusable configuration objects
Use dictionaries for:
- Quick prototyping and experimentation
- Simple, one-off configurations
- When you prefer a more concise syntax