Agent Serialization
Serialize and deserialize agents using to_dict/from_dict and save/load with a database.
Agent Serialization.
1"""2Agent Serialization3=============================45Agent Serialization.6"""78from kern.agent import Agent9from kern.db.sqlite import SqliteDb10from kern.models.openai import OpenAIResponses1112# ---------------------------------------------------------------------------13# Setup14# ---------------------------------------------------------------------------15agent_db = SqliteDb(db_file="tmp/agents.db")1617# ---------------------------------------------------------------------------18# Create Agent19# ---------------------------------------------------------------------------20agent = Agent(21 id="serialization-demo-agent",22 name="Serialization Demo Agent",23 model=OpenAIResponses(id="gpt-5.2"),24 db=agent_db,25)2627# ---------------------------------------------------------------------------28# Run Agent29# ---------------------------------------------------------------------------30if __name__ == "__main__":31 config = agent.to_dict()32 recreated = Agent.from_dict(config)3334 version = agent.save()35 loaded = Agent.load(id=agent.id, db=agent_db, version=version)3637 recreated.print_response("Say hello from a recreated agent.", stream=True)38 loaded.print_response("Say hello from a loaded agent.", stream=True)Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/14_advanced45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python agent_serialization.py