Skip to content

Got "TypeError: unsupported operand type(s) for +: 'int' and 'list'" from usage calculator #1813

Closed
@TrickyWhiteCat

Description

@TrickyWhiteCat

Initial Checks

Description

I ran into TypeError: unsupported operand type(s) for +: 'int' and 'list' when using gemini via GoogleModel with Gemini API.

Trace:

...
  File "...\.venv\Lib\site-packages\pydantic_ai\_agent_graph.py", line 337, in _make_request
    return self._finish_handling(ctx, model_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\.venv\Lib\site-packages\pydantic_ai\_agent_graph.py", line 361, in _finish_handling
    ctx.state.usage.incr(response.usage)
  File "...\.venv\Lib\site-packages\pydantic_ai\usage.py", line 48, in incr
    self.details[key] = self.details.get(key, 0) + value
                        ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
TypeError: unsupported operand type(s) for +: 'int' and 'list'

After some digging, I found that this error is prone to happen when there are repetitive requests, and the incr_usage.details value for each request is:

  • First request (success): {'thoughts_token_count': 70}
  • Second request (still success): {'thoughts_token_count': 68}
  • Thrid request (error): {'cache_tokens_details': [{'modality': <MediaModality.TEXT: 'TEXT'>, 'token_count': 3950}], 'cached_content_token_count': 3950, 'thoughts_token_count': 50}

It's clear that cache_tokens_details field is causing the problem, and this might be a Google problem, but some additional edge case handling is nice though.

Example Code

from __future__ import annotations as _annotations
import os
import asyncio

from pydantic import BaseModel

from pydantic_ai import Agent
from pydantic_ai.models.google import GoogleModel
from pydantic_ai.providers.google import GoogleProvider
from dotenv import load_dotenv
import logfire

load_dotenv()
logfire.configure(send_to_logfire=False)

provider = GoogleProvider(api_key=os.getenv("GOOGLE_GEMINI_API_KEY"))

model = GoogleModel(model_name="gemini-2.5-flash-preview-05-20", provider=provider)


class Test(BaseModel):
    output: str

email_writer_agent = Agent(
    model=model,
    output_type=Test,
    instructions=f"""
Here is a pretty long prompt to illustrate the usage of the Google Gemini API. You only need to response with a simple "okay" and nothing else. Ignore any non-sense below.

{"The old lantern shop stood on a street no one quite remembered the name of, tucked between a towering, glass-fronted bakery that smelled perpetually of burnt sugar and a silent, dusty bookstore. Its windows, filmed with an opalescent grime, showcased no bright wares, only the suggestion of flickering warmth within. Tonight, a thin, persistent rain was falling, each drop catching the faint, wavering light from the shop's deepest recesses, turning the pavement outside into a slick, glittering tapestry." * 50}
""",
    retries=1,
)

async def main():
    trials = 5
    for i in range(trials):

        result = await email_writer_agent.run("""Hi""")
        print(result.output)

if __name__ == '__main__':
    asyncio.run(main())

If you still not be able to replicate the problem, try increasing the string multiplier.

Python, Pydantic AI & LLM client version

Python 3.11.0

pydantic                                 2.11.4
pydantic-ai-slim                         0.2.6
pydantic_core                            2.33.2
pydantic-graph                           0.2.6
google-genai                             1.16.1

Metadata

Metadata

Assignees

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