AI Agent Response Templates: Consistent and Reliable Answers
AI Agent Response Templates: Consistent and Reliable Answers
📖 Keep reading
from jinja2 import Template
from datetime import datetime
# Define
ORDER_QUERY_TEMPLATE = Template("""
━━━━━━━━━━━━━━━━━━
{{ order_id }}
{{ product_name }}
¥{{ amount }}
{{ status }}
{{ create_time }}
━━━━━━━━━━━━━━━━━━
{% if status == '' %}
{{ estimate_date }}
{{ tracking_no }}
{% elif status == '' %}
{{ refund_progress }}%
{{ refund_date }}
{% endif %}
""")
def format_order_response(order_data: dict) -> str:
return ORDER_QUERY_TEMPLATE.render(
order_id=order_data.get('order_id', ''),
product_name=order_data.get('product_name', ''),
amount=order_data.get('amount', 0),
status=order_data.get('status', ''),
create_time=order_data.get('create_time', ''),
estimate_date=order_data.get('estimate_date', ''),
tracking_no=order_data.get('tracking_no', 'N/A'),
refund_progress=order_data.get('refund_progress', 0),
refund_date=order_data.get('refund_date', '')
)
|:----|:-----------|:-----------| || 30% | 98% | || 65% | 99% | || 72% | 91% | || 35% | 21% |
from pydantic import BaseModel
class OrderTemplateData(BaseModel):
order_id: str
amount: float
status: str # ///
def validate_and_render(template: Template, raw_data: dict) -> str:
try:
validated = OrderTemplateData(**raw_data)
return template.render(validated.model_dump())
except Exception as e:
return f"{e}{json.dumps(raw_data, ensure_ascii=False)}"
Related Articles
2026-08-06
Playwright AI Testing: Write E2E Tests in Plain English with MCP
2026-08-15
Best AI Email Writer for Real Estate Agents 2026: 7 Tools That Follow Up Without the Guilt
2026-07-17
AI Agent Log Rotation 2026
2026-07-19
AI Agent Rapid Prototyping: From Idea to Working Agent in One Hour
2026-08-06
Argo Workflows (16,878 Stars) 2026: Kubernetes-Native Pipeline Engine with DAG and Step Execution
2026-08-05
AI-Driven Competitor Analysis in 2026: Changedetection.io (33k Stars) + CrewAI - Track Rivals 24/7 for Free
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.