Skip to main content

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 the types 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 corresponding TypedDict variant with a Dict suffix:
  • types.Contenttypes.ContentDict
  • types.Parttypes.PartDict
  • types.GenerateContentConfigtypes.GenerateContentConfigDict
  • types.HttpOptionstypes.HttpOptionsDict
These TypedDict variants are useful for type hints when using dictionaries:

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
When using dictionaries, ensure that key names exactly match the Pydantic field names (usually in snake_case). Typos will not be caught until runtime.