Schema tools
schema_tools ¶
ADK tool wrappers for schema validation.
This module provides ADK-compatible tool functions that wrap existing schema validation utilities from schema_utils. These tools can be used by reflection agents to validate proposed schemas before returning them.
The tools return dict results compatible with ADK's FunctionTool pattern, enabling LLM agents to self-validate and retry on errors.
| ATTRIBUTE | DESCRIPTION |
|---|---|
validate_output_schema | ADK tool function that validates Pydantic schema definitions. Returns structured validation results as a dict, enabling LLM agents to validate proposed schemas and retry on errors. Never raises exceptions - all errors are returned in the result dict. TYPE: |
Examples:
Use validate_output_schema as an ADK tool:
from google.adk.agents import LlmAgent
from google.adk.tools import FunctionTool
from gepa_adk.utils.schema_tools import validate_output_schema
agent = LlmAgent(
name="schema_reflector",
model="gemini-2.5-flash",
instruction="Improve the schema. Validate before returning.",
tools=[FunctionTool(validate_output_schema)],
)
See Also
- [
gepa_adk.utils.schema_utils]: Core validation logic. - [
gepa_adk.engine.reflection_agents]: Reflection agent factories.
validate_output_schema ¶
Validate a Pydantic schema definition.
This function wraps validate_schema_text() to provide a tool-compatible interface for ADK agents. It returns structured validation results as a dict, enabling LLM agents to validate proposed schemas and retry on errors.
The function performs three-stage validation: 1. Preprocessing: Strip markdown code fences if present 2. Syntax: Parse Python syntax with ast.parse() 3. Structure: Check for security violations and BaseModel inheritance 4. Execution: Execute in controlled namespace and verify class
| PARAMETER | DESCRIPTION |
|---|---|
schema_text | Python code defining a Pydantic BaseModel class. May be wrapped in markdown code fences ( TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | dict with validation result: |
dict[str, Any] |
|
dict[str, Any] |
|
Examples:
Valid schema:
from gepa_adk.utils.schema_tools import validate_output_schema
schema = '''
class MySchema(BaseModel):
name: str
value: int
'''
result = validate_output_schema(schema)
assert result["valid"] is True
assert result["class_name"] == "MySchema"
assert result["field_names"] == ["name", "value"]
Invalid schema:
schema = '''
class BadSchema(BaseModel):
import os # Not allowed!
'''
result = validate_output_schema(schema)
assert result["valid"] is False
assert "stage" in result
assert "errors" in result
Tool usage in ADK agent:
from google.adk.agents import LlmAgent
from google.adk.tools import FunctionTool
agent = LlmAgent(
name="schema_validator",
model="gemini-2.5-flash",
instruction="Validate and improve schemas",
tools=[FunctionTool(validate_output_schema)],
)
See Also
validate_schema_text(): Core validation function.
Note
This function never raises exceptions - validation errors are returned in the dict result with valid=False. This ensures ADK tools can always return successfully and let the LLM handle the error.
Source code in src/gepa_adk/utils/schema_tools.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |