Serialization
Overview
Many components in Griptape can be serialized and deserialized using the to_dict and from_dict methods. There are also to_json and from_json as a convenience.
Here is how we can serialize an Agent
and then deserialize it back:
[03/12/25 20:36:02] INFO PromptTask d9e3f73477ef4a0aa71a670e543ee73c
Input: My name is Collin
INFO PromptTask d9e3f73477ef4a0aa71a670e543ee73c
Output: Hello, Collin! How can I assist you today?
[03/12/25 20:36:03] INFO PromptTask d9e3f73477ef4a0aa71a670e543ee73c
Input: What's my name?
[03/12/25 20:36:05] INFO PromptTask d9e3f73477ef4a0aa71a670e543ee73c
Output: Your name is Collin. How can I help you
further?
Serialization Overrides
All classes that implement the SerializableMixin can be serialized using the above methods.
However, only fields marked with metadata={"serializable": True}
will be included in the serialization process.
If you need to add or remove fields in the serialization process, you can pass serialization_overrides to any of the serialization methods.
[03/12/25 20:35:19] INFO PromptTask 4cb3fb0cb25b4fb5b9c1ec9fcc17789c
Input: My name is Collin
[03/12/25 20:35:20] INFO PromptTask 4cb3fb0cb25b4fb5b9c1ec9fcc17789c
Output: Hello, Collin! How can I assist you today?
{
│ 'type': 'Agent',
│ 'rulesets': [],
│ 'rules': [],
│ 'conversation_memory': {
│ │ 'type': 'ConversationMemory',
│ │ 'runs': [
│ │ │ {
│ │ │ │ 'type': 'Run',
│ │ │ │ 'id': '4ea9601351f542b6a403c82ffcf77656',
│ │ │ │ 'meta': None,
│ │ │ │ 'input': {
│ │ │ │ │ 'type': 'TextArtifact',
│ │ │ │ │ 'id': '80d96cb6f76e4d89925182d8b4e3acc7',
│ │ │ │ │ 'reference': None,
│ │ │ │ │ 'meta': {},
│ │ │ │ │ 'name': '80d96cb6f76e4d89925182d8b4e3acc7',
│ │ │ │ │ 'value': 'My name is Collin'
│ │ │ │ },
│ │ │ │ 'output': {
│ │ │ │ │ 'type': 'TextArtifact',
│ │ │ │ │ 'id': '501e6f2dfa3d496685ce9249bdb8121c',
│ │ │ │ │ 'reference': None,
│ │ │ │ │ 'meta': {'is_react_prompt': False},
│ │ │ │ │ 'name': '501e6f2dfa3d496685ce9249bdb8121c',
│ │ │ │ │ 'value': 'Hello, Collin! How can I assist you today?'
│ │ │ │ }
│ │ │ }
│ │ ],
│ │ 'meta': {},
│ │ 'max_runs': None
│ },
│ 'conversation_memory_strategy': 'per_structure',
│ 'max_meta_memory_entries': 20,
│ 'tasks': [
│ │ {
│ │ │ 'type': 'PromptTask',
│ │ │ 'rulesets': [],
│ │ │ 'rules': [],
│ │ │ 'id': '4cb3fb0cb25b4fb5b9c1ec9fcc17789c',
│ │ │ 'state': 'State.FINISHED',
│ │ │ 'parent_ids': [],
│ │ │ 'child_ids': [],
│ │ │ 'max_meta_memory_entries': 20,
│ │ │ 'context': {},
│ │ │ 'prompt_driver': {
│ │ │ │ 'type': 'OpenAiChatPromptDriver',
│ │ │ │ 'temperature': 0.1,
│ │ │ │ 'max_tokens': None,
│ │ │ │ 'stream': False,
│ │ │ │ 'extra_params': {},
│ │ │ │ 'base_url': None,
│ │ │ │ 'organization': None,
│ │ │ │ 'model': 'gpt-4o',
│ │ │ │ 'user': '',
│ │ │ │ 'response_format': None,
│ │ │ │ 'seed': None,
│ │ │ │ 'reasoning_effort': 'medium',
│ │ │ │ 'use_native_tools': True,
│ │ │ │ 'structured_output_strategy': 'native',
│ │ │ │ 'parallel_tool_calls': True,
│ │ │ │ 'modalities': [],
│ │ │ │ 'audio': {'voice': 'alloy', 'format': 'pcm16'}
│ │ │ },
│ │ │ 'tools': [],
│ │ │ 'max_subtasks': 20
│ │ }
│ ]
}
Types Overrides
Due to some unfortunate internals of the Griptape's serialization process, you may occasionally run into a NameError
when serializing. It will look something like this:
This is something we're actively working on fixing, but in the meantime, you can use the types_overrides parameter to pass in a dictionary of types that need to be overridden during serialization.
Here is an example of how you can use types_overrides
:
from duckduckgo_search import DDGS
from rich.pretty import pprint
from griptape.drivers.web_search import BaseWebSearchDriver
from griptape.drivers.web_search.duck_duck_go import DuckDuckGoWebSearchDriver
from griptape.tools import WebSearchTool
web_driver = DuckDuckGoWebSearchDriver()
web_tool = WebSearchTool(web_search_driver=web_driver)
web_tool_dict = web_tool.to_dict(
serializable_overrides={"web_search_driver": True},
types_overrides={"BaseWebSearchDriver": BaseWebSearchDriver, "DDGS": DDGS},
)
pprint(web_tool_dict)