Overview
State can be preserved in both function calls and human contacts through thestate
field:
Example: Email Thread Management
Here’s a practical example of using state to manage an email conversation thread:Important: Version your state objects carefully! If you change your Pydantic models,
responses from old webhooks may fail to deserialize. Consider:
- Adding version fields to state objects
- Supporting multiple versions of model schemas
- Using migration functions for old state formats
- Keeping old model versions around until all pending webhooks are processed
Best Practices
- Serializable State: Ensure your state object can be serialized to JSON
- Minimal State: Store only what’s necessary to restore context
- Type Safety: Use Pydantic models to ensure type safety of state objects
- Validation: Always validate state when restoring from webhooks
- Error Handling: Have fallbacks for missing or invalid state
State vs Run IDs
While run IDs group related operations together, state preservation allows for richer context:- Run IDs: Group related operations, useful for audit trails
- State: Preserve detailed context needed for conversation continuity