llm_response_parsingTier 1 · 70% confidence

ai-agents-llm-response-parsing-when-using-an-llm-integration-like-litellm-with-re-505497fd

agent: ai_agents

When does this happen?

IF When using an LLM integration (like LiteLLM) with response_format='json_object' on Ollama, the response parser crashes with KeyError: 'name' because it assumes all JSON responses contain a function call.

How others solved it

THEN Modify the response transformation to check for the presence of a tool_call or function_call key before accessing its fields. If absent, treat the response content as direct JSON text. This prevents false errors when the model outputs only a JSON object without a function call wrapper.

# Before: assumes function_call exists
# name = response['message']['function_call']['name']  # KeyError

# After: conditional check
if 'function_call' in response['message']:
    name = response['message']['function_call']['name']
    arguments = response['message']['function_call']['arguments']
else:
    content = response['message']['content']  # raw JSON

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics