Serialization
How AgentForge serializes tool inputs, outputs, and results for LLM communication.
How It Works
Tool results are serialized to strings before being sent back to the LLM as ToolResultMessage:
- Objects/arrays —
JSON.stringify() - Strings — passed as-is
- Numbers/booleans — converted to string
- Errors — formatted as error messages with
isError: true
Input Deserialization
The LLM sends tool inputs as JSON. AgentForge:
- Parses the JSON from the LLM response
- Validates against the Zod input schema
- Passes the typed, validated object to
execute()
Output Serialization
After execute() returns:
- Validates the output against the Zod output schema
- Serializes to JSON string
- Includes in the
ToolResultMessage.contentfield
// Tool returns:
{ orderId: 'abc', total: 29.99 }
// LLM receives as ToolResultMessage:
{
role: 'tool',
toolCallId: 'call_123',
name: 'create-order',
content: '{"orderId":"abc","total":29.99}',
isError: false
}Error Serialization
When a tool throws:
{
role: 'tool',
toolCallId: 'call_123',
name: 'create-order',
content: 'Error: Order service unavailable',
isError: true
}The LLM sees the error and can decide to retry, use a different approach, or report the error to the user.