The response format processing system handles extraction and manipulation of structured outputs from AI agent blocks. This system enables blocks to define expected output schemas (JSON Schema format) and provides utilities for extracting specific fields from structured responses, parsing configurations, and formatting values for display. The processing occurs during block execution and when displaying results in the workspace interface.
For information about agent block configuration and structured outputs, see Agent Blocks. For details on the overall execution flow, see Workflow Execution Engine.
The response format system bridges the gap between AI model outputs and the structured data required by downstream workflow blocks. It provides a schema-based approach to validating and extracting fields from LLM responses.
Key Functions:
Sources: apps/sim/lib/core/utils/response-format.ts1-220
Diagram: Response Format Schema Processing Flow
Sources: apps/sim/lib/core/utils/response-format.ts14-49
The Field interface defines the structure of individual output fields:
Sources: apps/sim/lib/core/utils/response-format.ts5-9
Output IDs uniquely identify field selections within a block's response:
| Format | Example | Description |
|---|---|---|
| Block-level | blockId | References entire block output |
| Field-level (underscore) | blockId_fieldName | References specific field (legacy) |
| Field-level (dot) | blockId.fieldName | References specific field |
| Nested field | blockId_result.data.value | References nested field path |
Sources: apps/sim/lib/core/utils/response-format.ts129-141
Diagram: Response Format Parsing Logic
The parseResponseFormatSafely function handles multiple input formats with error tolerance:
null for missing values<blockId.field>Sources: apps/sim/lib/core/utils/response-format.ts52-80
Diagram: Field Extraction from Schema
This function handles two schema formats:
Legacy Format:
JSON Schema Format:
Sources: apps/sim/lib/core/utils/response-format.ts14-49
This function extracts specific field values from parsed JSON based on selected output paths:
Diagram: Field Value Extraction Process
Example Usage:
Sources: apps/sim/lib/core/utils/response-format.ts82-111
The system provides two path traversal functions:
Internal Traversal (traverseObjectPathInternal):
undefined if path doesn't existPublic Traversal (traverseObjectPath):
Diagram: Path Traversal Logic
Sources: apps/sim/lib/core/utils/response-format.ts191-219
Handles safe parsing of output content that may be either a string or an object:
| Input Type | Action | Result |
|---|---|---|
Object with string content | Parse JSON string | Parsed object |
Object with object content | Return as-is | Original object |
| Invalid JSON string | Return original | Original structure |
No content field | Return as-is | Original object |
Error Handling: The function never throws exceptions. If JSON parsing fails, it returns the original structure to preserve data integrity.
Sources: apps/sim/lib/core/utils/response-format.ts144-162
Diagram: Output ID Utility Functions
Function Descriptions:
| Function | Purpose | Example |
|---|---|---|
extractBlockIdFromOutputId(outputId) | Extracts block ID from output ID | "agent_result" → "agent" |
extractPathFromOutputId(outputId, blockId) | Extracts field path after block ID | "agent_result.data" → "result.data" |
hasResponseFormatSelection(selectedOutputs, blockId) | Checks if any field selections exist | Returns true if underscore-separated IDs present |
getSelectedFieldNames(selectedOutputs, blockId) | Gets list of selected field paths | Returns ["result.title", "result.count"] |
Sources: apps/sim/lib/core/utils/response-format.ts129-184
Converts extracted field values into a display-ready string format:
Diagram: Field Value Formatting Process
Example:
Sources: apps/sim/lib/core/utils/response-format.ts113-126
The response format system integrates with the execution engine at several points:
Diagram: Response Format in Execution Flow
Sources: apps/sim/lib/core/utils/response-format.ts1-220
The response format system implements defensive error handling:
| Scenario | Handling | Result |
|---|---|---|
Invalid JSON in parseResponseFormatSafely | Log warning, return null | Execution continues |
Missing field in extractFieldValues | Skip field silently | Extraction continues |
Invalid path in traverseObjectPath | Return undefined | No error thrown |
Parse failure in parseOutputContentSafely | Return original structure | Data preserved |
The system uses @sim/logger for diagnostic output:
Log Levels:
Sources: apps/sim/lib/core/utils/response-format.ts1-78
Sources: apps/sim/lib/core/utils/response-format.ts52-184
The system provides TypeScript type definitions for all public APIs:
Type Flexibility: Functions accept any types for maximum compatibility with dynamic JSON structures while providing type-safe return values.
Sources: apps/sim/lib/core/utils/response-format.ts5-90
traverseObjectPath is called| Function | Time Complexity | Notes |
|---|---|---|
parseResponseFormatSafely | O(n) | Where n = JSON string length |
extractFieldsFromSchema | O(f) | Where f = number of fields |
extractFieldValues | O(s × d) | Where s = selected outputs, d = path depth |
traverseObjectPath | O(d) | Where d = path depth |
formatFieldValues | O(f) | Where f = number of fields |
Sources: apps/sim/lib/core/utils/response-format.ts1-220
The response format processing system interacts with:
Refresh this wiki
This wiki was recently refreshed. Please wait 6 days to refresh again.