haive.core.schema.state_schema¶
State schema base class for the Haive framework.
from typing import Any This module provides the StateSchema base class that extends Pydantic’s BaseModel with features specifically designed for AI agent state management and graph-based workflows. The StateSchema class adds powerful capabilities including field sharing between parent and child graphs, reducer functions for state updates, engine I/O tracking, and extensive serialization support.
StateSchema serves as the foundation of the Haive Schema System, enabling fully dynamic and serializable state schemas that can be composed, modified, and extended at runtime. This flexibility makes it ideal for complex agent architectures and nested workflows.
Key features include: - Field sharing: Share state between parent and child graphs with explicit control - Reducer functions: Define how field values should be combined during state updates - Engine I/O tracking: Map which fields are inputs and outputs for specific engines - Message handling: Built-in methods for working with message fields - Serialization: Comprehensive support for converting to/from dictionaries and JSON - State manipulation: Methods for updating, merging, and comparing states - Pretty printing: Rich visualization of state content - Engine integration: Prepare inputs and process outputs for specific engines
Examples
from typing import List from langchain_core.messages import BaseMessage, HumanMessage, AIMessage from pydantic import Field from haive.core.schema import StateSchema from langgraph.graph import add_messages
- class ConversationState(StateSchema):
messages: List[BaseMessage] = Field(default_factory=list) query: str = Field(default=””) response: str = Field(default=””) context: List[str] = Field(default_factory=list)
# Define which fields should be shared with parent graphs __shared_fields__ = [“messages”]
# Define reducer functions for each field __reducer_fields__ = {
“messages”: add_messages, “context”: lambda a, b: (a or []) + (b or [])
}
# Define which fields are inputs/outputs for which engines __engine_io_mappings__ = {
- “retriever”: {
“inputs”: [“query”], “outputs”: [“context”]
}, “llm”: {
“inputs”: [“query”, “context”, “messages”], “outputs”: [“response”]
}
}
Classes¶
Configuration for engine input/output mappings. |
|
Configuration for state schema metadata. |
|
Enhanced base class for state schemas in the Haive framework. |
Module Contents¶
- class haive.core.schema.state_schema.EngineIOConfig[source]¶
Bases:
typing_extensions.TypedDict
Configuration for engine input/output mappings.
Initialize self. See help(type(self)) for accurate signature.
- class haive.core.schema.state_schema.StateConfig[source]¶
Bases:
typing_extensions.TypedDict
Configuration for state schema metadata.
Initialize self. See help(type(self)) for accurate signature.
- class haive.core.schema.state_schema.StateSchema(/, **data)[source]¶
Bases:
pydantic.BaseModel
,Generic
[TEngine
,TEngines
]Enhanced base class for state schemas in the Haive framework.
StateSchema extends Pydantic’s BaseModel with features for AI agent state management and graph-based workflows. It serves as the core component of the Haive Schema System, providing extensive capabilities for state management in complex agent architectures.
- Key Features:
Field sharing: Control which fields are shared between parent and child graphs
Reducer functions: Define how field values are combined during state updates
Engine I/O tracking: Map which fields are inputs/outputs for which engines
Message handling: Methods for working with conversation message fields
Serialization: Convert states to/from dictionaries and JSON
State manipulation: Update, merge, compare, and diff state objects
Integration: Support for LangGraph and engine components
Visualization: Rich display options for state inspection
- Special Class Variables:
__shared_fields__ (List[str]): Fields to share with parent graphs __serializable_reducers__ (Dict[str, str]): Serializable reducer function names __engine_io_mappings__ (Dict[str, Dict[str, List[str]]]): Engine I/O mappings __input_fields__ (Dict[str, List[str]]): Input fields for each engine __output_fields__ (Dict[str, List[str]]): Output fields for each engine __structured_models__ (Dict[str, str]): Paths to structured output models __structured_model_fields__ (Dict[str, List[str]]): Fields for structured models __reducer_fields__ (Dict[str, Callable]): Runtime reducer functions (not stored)
Field sharing enables parent and child graphs to maintain synchronized state for specific fields, which is critical for nested graph execution. Reducer functions define how field values are combined during updates, enabling sophisticated state merging operations beyond simple assignment.
Examples
from typing import List from langchain_core.messages import BaseMessage from pydantic import Field from haive.core.schema import StateSchema
- class MyState(StateSchema):
messages: List[BaseMessage] = Field(default_factory=list) query: str = Field(default=””) result: str = Field(default=””)
# Share only messages with parent graphs __shared_fields__ = [“messages”]
# Define reducer for messages __reducer_fields__ = {
“messages”: add_messages # From langgraph.graph
}
# Create state instance state = MyState()
# Add a message state.add_message(HumanMessage(content=”Hello”))
# Convert to dictionary state_dict = state.to_dict()
# Create from dictionary new_state = MyState.from_dict(state_dict)
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)
- add_engine(name, engine)[source]¶
Add an engine to the engines registry.
- Parameters:
name (str) – Name/key for the engine
engine (haive.core.engine.base.Engine) – Engine instance to add
- Return type:
None
- add_message(message)[source]¶
Add a single message to the messages field.
- Parameters:
message (langchain_core.messages.BaseMessage) – BaseMessage to add
- Returns:
Self for chaining
- Return type:
- add_messages(new_messages)[source]¶
Add multiple messages to the messages field.
- Parameters:
new_messages (list[langchain_core.messages.BaseMessage]) – List of messages to add
- Returns:
Self for chaining
- Return type:
- apply_reducers(other)[source]¶
Update state applying reducer functions where defined.
This method processes updates with special handling for fields that have reducer functions defined.
- Parameters:
other (dict[str, Any] | StateSchema) – Dictionary or StateSchema with update values
- Returns:
Self for chaining
- Return type:
- classmethod as_table()[source]¶
Create a rich table representation of the schema.
- Returns:
Rich Table object
- Return type:
rich.table.Table
- clear_messages()[source]¶
Clear all messages in the messages field.
- Returns:
Self for chaining
- Return type:
- combine_with(other)[source]¶
Combine this state with another, applying reducers for shared fields.
This is more sophisticated than update() or apply_reducers() as it properly handles StateSchema-specific metadata and shared fields.
- Parameters:
other (StateSchema | dict[str, Any]) – Other state to combine with
- Returns:
New combined state instance
- Return type:
- classmethod compare_with(other, title=None)[source]¶
Compare this schema with another in a side-by-side display.
- Parameters:
other (type[StateSchema]) – Other schema to compare with
title (str | None) – Optional title for the comparison
- Return type:
None
- copy(**updates)[source]¶
Create a copy of this state, optionally with updates.
- Parameters:
**updates – Field values to update in the copy
- Returns:
New StateSchema instance
- Return type:
- classmethod create_input_schema(engine_name=None, name=None)[source]¶
Alias for derive_input_schema for backward compatibility.
- classmethod create_output_schema(engine_name=None, name=None)[source]¶
Alias for derive_output_schema for backward compatibility.
- deep_copy()[source]¶
Create a deep copy of this state object.
- Returns:
New StateSchema instance with deep-copied values
- Return type:
- classmethod derive_input_schema(engine_name=None, name=None)[source]¶
Derive an input schema for the given engine from this state schema.
This method intelligently selects the appropriate base class for the derived schema, using prebuilt states (MessagesState, ToolState) when appropriate instead of just creating a generic BaseModel.
- Parameters:
- Returns:
A BaseModel subclass for input validation, potentially inheriting from MessagesState or ToolState for better compatibility
- Return type:
type[pydantic.BaseModel]
- classmethod derive_output_schema(engine_name=None, name=None)[source]¶
Derive an output schema for the given engine from this state schema.
This method intelligently selects the appropriate base class for the derived schema, using prebuilt states (MessagesState, ToolState) when appropriate instead of just creating a generic BaseModel.
- Parameters:
- Returns:
A BaseModel subclass for output validation, potentially inheriting from MessagesState or ToolState for better compatibility
- Return type:
type[pydantic.BaseModel]
- classmethod display_code(title=None)[source]¶
Display Python code representation of the schema.
- Parameters:
title (str | None) – Optional title for the display
- Return type:
None
- classmethod display_schema(title=None)[source]¶
Display schema information in a rich format.
- Parameters:
title (str | None) – Optional title for the display
- Return type:
None
- classmethod extract_values(state, keys=None)[source]¶
Class method to extract values from a state object or dictionary.
- Parameters:
- Returns:
Dictionary containing the requested values
- Return type:
- classmethod from_dict(data)[source]¶
Create a state from a dictionary.
- Parameters:
data (FieldMapping) – Dictionary with field values
- Returns:
New StateSchema instance
- Return type:
Self
- classmethod from_json(json_str)[source]¶
Create state from JSON string.
- Parameters:
json_str (str) – JSON string to parse
- Returns:
New StateSchema instance
- Return type:
- classmethod from_partial_dict(data)[source]¶
Create a state from a partial dictionary, filling in defaults.
- classmethod from_runnable_config(config)[source]¶
Extract state from a RunnableConfig.
- Parameters:
config (langchain_core.runnables.RunnableConfig) – RunnableConfig to extract from
- Returns:
StateSchema instance or None if no state found
- Return type:
StateSchema | None
- classmethod from_snapshot(snapshot)[source]¶
Create a state from a LangGraph StateSnapshot.
- Parameters:
snapshot (Any) – StateSnapshot from LangGraph
- Returns:
New StateSchema instance
- Return type:
- get(key, default=None)[source]¶
Safely get a field value with a default.
- Parameters:
key (str) – Field name to get
default (Any) – Default value if field doesn’t exist
- Returns:
Field value or default
- Return type:
Any
- classmethod get_class_engine(name)[source]¶
Get a class-level engine by name.
- Parameters:
name (str) – Name of the engine to retrieve
- Returns:
Engine instance if found, None otherwise
- Return type:
Any | None
- get_engine(name)[source]¶
Get an engine by name.
- Parameters:
name (str) – Name of the engine to retrieve
- Returns:
Engine instance if found, None otherwise
- Return type:
haive.core.engine.base.Engine | None
- get_instance_engine(name)[source]¶
Get an engine from instance or class level.
- Parameters:
name (str) – Name of the engine to retrieve
- Returns:
Engine instance if found, None otherwise
- Return type:
Any | None
- get_last_message()[source]¶
Get the last message in the messages field.
- Returns:
Last message or None if no messages exist
- Return type:
langchain_core.messages.BaseMessage | None
Check if a field is shared with parent graphs.
- classmethod manager()[source]¶
Get a manager for this schema (shorthand for to_manager()).
- Returns:
StateSchemaManager instance
- Return type:
- merge_engine_output(engine_name, output, apply_reducers=True)[source]¶
Merge output from an engine into this state.
- Parameters:
- Returns:
Self for chaining
- Return type:
- merge_messages(new_messages)[source]¶
Merge new messages with existing messages using appropriate reducer.
- Parameters:
new_messages (list[langchain_core.messages.BaseMessage]) – New messages to add
- Returns:
Self for chaining
- Return type:
- model_dump(**kwargs)[source]¶
Override model_dump to exclude internal fields and handle special types.
- Parameters:
**kwargs (Any) – Keyword arguments for model_dump
- Returns:
Dictionary representation of the state
- Return type:
- model_post_init(__context)[source]¶
Sync engines from class level to instance level after initialization.
This ensures that engines stored at the class level (via SchemaComposer) are available on state instances.
- Parameters:
__context (Any)
- Return type:
None
- patch(update_data, apply_reducers=True)[source]¶
Update specific fields in the state.
- Parameters:
- Returns:
Self for chaining
- Return type:
- prepare_for_engine(engine_name)[source]¶
Prepare state data for a specific engine.
Extracts only fields that are inputs for the specified engine.
- pretty_print(title=None)[source]¶
Print state with rich formatting for easy inspection.
- Parameters:
title (str | None) – Optional title for the display
- Return type:
None
- setup_engines_and_tools()[source]¶
Setup engines and sync their tools, structured output models, and add engine to state.
This validator runs after the model is created and: 1. Finds all engine fields in the state 2. Syncs engine to main engine field and engines dict 3. Syncs tools from engine to state tools field 4. Syncs structured output models 5. Sets up parent-child relationships for nested state schemas
- Return type:
Self
Get the list of fields shared with parent graphs.
- sync_engine_fields()[source]¶
Sync between engine and engines dict for backward compatibility.
This validator ensures that: 1. If ‘engine’ is set, it’s available in engines dict 2. If engines dict has items but no engine, set main engine 3. Both access patterns work seamlessly
- Return type:
Self
- to_command(goto=None, graph=None)[source]¶
Convert state to a Command object for LangGraph control flow.
- to_dict()[source]¶
Convert the state to a clean dictionary.
- Returns:
Dictionary representation of the state
- Return type:
- to_json()[source]¶
Convert state to JSON string.
- Returns:
JSON string representation of the state
- Return type:
- classmethod to_manager(name=None)[source]¶
Convert schema class to a StateSchemaManager for further manipulation.
- Parameters:
name (str | None) – Optional name for the resulting manager
- Returns:
StateSchemaManager instance
- Return type:
- classmethod to_python_code()[source]¶
Convert schema to Python code representation.
- Returns:
String containing Python code representation
- Return type:
- to_runnable_config(thread_id=None, **kwargs)[source]¶
Convert state to a RunnableConfig.
- Parameters:
thread_id (str | None) – Optional thread ID for the configuration
**kwargs – Additional configuration parameters
- Returns:
RunnableConfig containing state data
- Return type:
langchain_core.runnables.RunnableConfig
- update(other)[source]¶
Update the state with values from another state or dictionary.
This method performs a simple update without applying reducers.
- Parameters:
other (dict[str, Any] | StateSchema) – Dictionary or StateSchema with update values
- Returns:
Self for chaining
- Return type:
- classmethod validate_engine(v)[source]¶
Handle both serialized dict and actual Engine instances.
This validator allows the engine field to accept both: - Actual Engine instances (for runtime use) - Serialized dicts (for state passing between agents)
This prevents the “Can’t instantiate abstract class Engine” error when deserializing state in multi-agent systems.
- Return type:
Any
- classmethod validate_engines(v)[source]¶
Handle both serialized dicts and actual Engine instances in engines dict.
Similar to validate_engine but for the engines dictionary. Each value can be either a serialized dict or an actual Engine instance.
- Return type:
Any
Create a copy of this schema with specified shared fields.