Error when using LiteLLM with non-OpenAI models
rowboat_agents-1 | === Error in stream event processing === rowboat_agents-1 | Error: 'NoneType' object has no attribute 'total_tokens' rowboat_agents-1 | Event details: rowboat_agents-1 | Event type: raw_response_event rowboat_agents-1 | Event attributes: {'data': ResponseCompletedEvent(response=Response(id='fake_id', created_at=1746795180.6707258, error=None, incomplete_details=None, instructions=None, metadata=None, model='claude-3-7-sonnet-latest', object='response', output=[ResponseOutputMessage(id='fake_id', content=[ResponseOutputText(annotations=[], text="Sender agent: Article Workflow Hub\nContent: Hello! I'm here to help you create a high-quality article. Would you like me to guide you through the process of writing an article on a topic of your choice? Just let me know what topic you'd like an article about, and I'll coordinate the writing and review process for you.", type='output_text')], role='assistant', status='completed', type='message')], parallel_tool_calls=False, temperature=0.0, tool_choice='auto', tools=[], top_p=None, max_output_tokens=None, previous_response_id=None, reasoning=None, service_tier=None, status=None, text=None, truncation=None, usage=None, user=None), type='response.completed'), 'type': 'raw_response_event'} rowboat_agents-1 | Full event object: RawResponsesStreamEvent(data=ResponseCompletedEvent(response=Response(id='fake_id', created_at=1746795180.6707258, error=None, incomplete_details=None, instructions=None, metadata=None, model='claude-3-7-sonnet-latest', object='response', output=[ResponseOutputMessage(id='fake_id', content=[ResponseOutputText(annotations=[], text="Sender agent: Article Workflow Hub\nContent: Hello! I'm here to help you create a high-quality article. Would you like me to guide you through the process of writing an article on a topic of your choice? Just let me know what topic you'd like an article about, and I'll coordinate the writing and review process for you.", type='output_text')], role='assistant', status='completed', type='message')], parallel_tool_calls=False, temperature=0.0, tool_choice='auto', tools=[], top_p=None, max_output_tokens=None, previous_response_id=None, reasoning=None, service_tier=None, status=None, text=None, truncation=None, usage=None, user=None), type='response.completed'), type='raw_response_event') rowboat_agents-1 | Traceback: Traceback (most recent call last): rowboat_agents-1 | File "/app/src/graph/core.py", line 199, in run_turn_streamed rowboat_agents-1 | tokens_used["total"] += event.data.response.usage.total_tokens rowboat_agents-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rowboat_agents-1 | AttributeError: 'NoneType' object has no attribute 'total_tokens' rowboat_agents-1 | rowboat_agents-1 | ================================================== rowboat_agents-1 | Traceback (most recent call last): rowboat_agents-1 | File "/app/src/graph/core.py", line 199, in run_turn_streamed rowboat_agents-1 | tokens_used["total"] += event.data.response.usage.total_tokens rowboat_agents-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rowboat_agents-1 | AttributeError: 'NoneType' object has no attribute 'total_tokens' rowboat_agents-1 | rowboat_agents-1 | Error in stream processing: 'NoneType' object has no attribute 'total_tokens' rowboat_agents-1 | [2025-05-09 12:53:01 +0000] [1] [INFO] 172.18.0.4:37460 POST /chat_stream 1.1 200 - 2047675
You are encountering this error because the code is trying to access event.data.response.usage.total_tokens at apps/rowboat_agents/src/graph/core.py , but event.data.response.usage is None. This happens when the response object does not include usage information.
Fix: Adding a guard clause before accessing total_tokens to prevent the AttributeError when usage is None.
from : apps/rowboat_agents/src/graph/core.py
if hasattr(event.data, 'type') and event.data.type == "response.completed" and hasattr(event.data.response, 'usage'):
tokens_used["total"] += event.data.response.usage.total_tokens
tokens_used["prompt"] += event.data.response.usage.input_tokens
tokens_used["completion"] += event.data.response.usage.output_tokens
to :
if hasattr(event.data, 'type') and event.data.type == "response.completed" and hasattr(event.data.response, 'usage'):
usage = getattr(event.data.response, "usage", None)
if usage and getattr(usage, "total_tokens", None) is not None:
tokens_used["total"] += usage.total_tokens
if usage and getattr(usage, "prompt_tokens", None) is not None:
tokens_used["prompt"] += usage.prompt_tokens
if usage and getattr(usage, "completion_tokens", None) is not None:
tokens_used["completion"] += usage.completion_tokens
怎么使用非openai的模型,我想使用gemini