haive.core.schema.prebuilt.structured_output_state¶
State schema with structured output parsing capabilities using LangChain output parsers.
Classes¶
Mixin to add structured output capabilities to any state schema. |
|
MessagesState with automatic structured output parsing and token tracking. |
Module Contents¶
- class haive.core.schema.prebuilt.structured_output_state.StructuredOutputMixin[source]¶
Mixin to add structured output capabilities to any state schema.
This mixin can be used to add structured output parsing to custom state schemas without inheriting from StructuredOutputState.
- class haive.core.schema.prebuilt.structured_output_state.StructuredOutputState(/, **data)[source]¶
Bases:
haive.core.schema.prebuilt.messages.messages_with_token_usage.MessagesStateWithTokenUsageMessagesState with automatic structured output parsing and token tracking.
This state schema extends MessagesStateWithTokenUsage to automatically parse AI messages into structured outputs using LangChain output parsers. It leverages the PydanticToolsParser to convert Pydantic models into tool call messages, maintaining proper message flow and token tracking.
Key features: - Automatic parsing of AI messages with structured output - Conversion of Pydantic models to tool call messages - Token usage tracking for all messages including parsed outputs - Support for multiple output parser types - Field validator integration for seamless parsing
Examples
from pydantic import BaseModel
- class SearchQuery(BaseModel):
query: str filters: Dict[str, Any]
# Configure state with output model state = StructuredOutputState(
output_models=[SearchQuery], parse_as_tools=True # Convert to tool calls
)
# AI message with structured output gets parsed automatically ai_msg = AIMessage(
content=’{“query”: “python”, “filters”: {“language”: “en”}}’, response_metadata={“token_usage”: {“total_tokens”: 50}}
) state.messages.append(ai_msg)
# Automatically creates ToolMessage with parsed content # Token usage is tracked for both original and parsed messages
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)
- format_for_structured_output()[source]¶
Get format instructions for the configured output models.
- Return type:
- get_latest_parsed_output()[source]¶
Get the most recent parsed output.
- Return type:
pydantic.BaseModel | None
- get_parsed_output(message_index)[source]¶
Get parsed output for a specific message index.
- Parameters:
message_index (int)
- Return type:
pydantic.BaseModel | None
- get_tool_calls()[source]¶
Get all tool call messages from the conversation.
- Return type:
list[langchain_core.messages.ToolMessage]
- classmethod parse_structured_outputs(messages, info)¶
Parse AI messages with structured output into appropriate format.