-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
documentationImprovements or additions to documentationImprovements or additions to documentation
Description
def format_pydantic_error_for_llm(error: ValidationError) -> str:
"""
Processes a Pydantic ValidationError, handling nested structures,
and formats it into a concise, LLM-friendly string.
"""
error_summary = "Function Call Validation Failed. Must correct the arguments based on the following issues:\n"
for error_item in error.errors():
# **IMPROVED LOGIC FOR NESTING:**
# Join the elements of 'loc' to form a clear path (e.g., 'items[0].id')
path_parts = []
for part in error_item['loc']:
if isinstance(part, int):
# If the part is an index, use bracket notation: [0]
path_parts.append(f"[{part}]")
else:
# If the part is a field name, use dot notation or just the name
if path_parts and not path_parts[-1].startswith('['):
path_parts.append(f".{part}")
else:
path_parts.append(str(part))
# Join the parts into a single string (e.g., "items[0].id")
field_path = "".join(path_parts).strip('.') # Strip leading dot if present
# --- Remaining Logic is similar to your original ---
error_type = error_item['type']
message = error_item['msg']
if error_type == 'literal_error':
permitted_values = error_item['ctx'].get('expected', 'Unknown')
error_details = f"Value is not in the permitted list: {permitted_values}"
elif error_type.startswith('type_error'):
expected_type = message.split(' ')[-1].strip('.')
error_details = f"Expected type was {expected_type}"
elif error_type == 'greater_than':
expected = error_item['ctx'].get('gt', '0')
error_details = f"Value must be greater than {expected}"
else:
error_details = message
error_summary += f"- Parameter **`{field_path}`**: {error_details}\n"
return error_summary.strip()Metadata
Metadata
Assignees
Labels
documentationImprovements or additions to documentationImprovements or additions to documentation