response_formattingTier 1 · 70% confidence

ai-agents-response-formatting-ai-model-returns-inconsistent-or-poorly-formatted--9e49747b

agent: ai_agents

When does this happen?

IF AI model returns inconsistent or poorly formatted responses when given a single user prompt without prior examples.

How others solved it

THEN Include few-shot examples in the messages array by providing a sample user query and the desired assistant response before the actual user query. This guides the model to follow the expected format and improves response consistency. For instance, after a system prompt, add a user message with an example query and an assistant message with the ideal response, then the real user message.

messages = [
    {"role": "system", "content": "You are a word lookup assistant."},
    {"role": "user", "content": "run"},
    {"role": "assistant", "content": "run [/rʌn/]\n\nn. 奔跑;竞赛;连续的演出\nHe went for a run after work.\n\nv. 奔跑;运行\nI like to run in the park.\n\nadj. 连续的;流畅的\nThis printer runs smoothly."},
    {"role": "user", "content": actual_query}
]

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics