Skip to content

[BUG] OpenRouter usage has different schema to OpenAI's usage, causing attribute error. #1401

Open
@sentienthouseplant

Description

@sentienthouseplant

Describe the bug
Sometimes when using the openrouter API endpoint with the OpenAIServerModel you will get the following error:

Error in generating model output:
'NoneType' object has no attribute 'prompt_tokens'

This is because openrouter doesn't guarantee the usage key.

Here is the openrouter API page: https://openrouter.ai/docs/api-reference/overview#completionsresponse-format
Here is the openapi page: https://platform.openai.com/docs/api-reference/chat/object

Code to reproduce the error
Use the openrouter API endpoint with the OpenAIServerModel, occasionally this error will materialize.

Error logs (if any)

Error in generating model output:
'NoneType' object has no attribute 'prompt_tokens'

Expected behavior
Defaults to 0 when response.usage is None.

Packages version:
smolagents 1.17.0

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions