Skip to content

Error adding custom provider (perplexity) "unsupported operand type(s) for +=: 'int' and 'str'" #42

@dintho

Description

@dintho

Hi, i am having some issues with adding in perplexity AI as a custom provider

error from ui

unsupported operand type(s) for +=: 'int' and 'str'

deployment

docker container

]# docker exec -it aa5a6b10ffe7 /bin/bash
llms@aa5a6b10ffe7:/app$ llms
usage: llms [-h] [--config FILE] [--providers FILE] [-m MODEL] [--chat REQUEST] [-s PROMPT] [--tools TOOLS] [--image IMAGE] [--audio AUDIO] [--file FILE] [--out MODALITY] [--args PARAMS] [--raw] [--nohistory] [--nostore] [--list] [--check PROVIDER]
            [--serve PORT] [--enable PROVIDER] [--disable PROVIDER] [--default MODEL] [--init] [--update-providers] [--reset [TYPE]] [--auth EXTENSION] [--logprefix PREFIX] [--verbose] [--add [EXTENSION]] [--remove [EXTENSION]] [--update [EXTENSION]]
            [--adduser USERNAME] [--removeuser USERNAME] [--listusers] [--lockuser [USERNAME]] [--unlockuser USERNAME]

llms v3.0.42

configs

i added this to llms.json

    "perplexity": {
      "enabled": true,
      "id": "perplexity",
      "npm": "@ai-sdk/openai-compatible",
      "api": "https://api.perplexity.ai",
      "no_tools": true,
      "env": [
        "PERPLEXITY_API_KEY"
      ],
      "models": {
        "sonar": {
          "id": "sonar",
          "name": "Perplexity Sonar"
        },
        "sonar-pro": {
          "id": "sonar-pro",
          "name": "Perplexity Sonar Pro"
        },
        "sonar-deep-research": {
          "id": "sonar-deep-research",
          "name": "Perplexity Sonar Deep Research"
        }
      }
    }

Errorr logs

2026-04-08 06:22:43.604289+00:00======== Running on http://0.0.0.0:30335 ========
2026-04-08 06:22:43.604330+00:00(Press CTRL+C to quit)
2026-04-08 06:23:24.107246+00:00provider: perplexity OpenAiCompatible
2026-04-08 06:23:24.109400+00:00POST https://api.perplexity.ai/chat/completions
2026-04-08 06:23:24.109583+00:00{
2026-04-08 06:23:24.109631+00:00"model": "sonar",
2026-04-08 06:23:24.109655+00:00"messages": [
2026-04-08 06:23:24.109678+00:00{
2026-04-08 06:23:24.109701+00:00"timestamp": 1775628830151,
2026-04-08 06:23:24.109743+00:00"role": "user",
2026-04-08 06:23:24.109766+00:00"content": [
2026-04-08 06:23:24.109789+00:00{
2026-04-08 06:23:24.109812+00:00"type": "text",
2026-04-08 06:23:24.109836+00:00"text": "hi"
2026-04-08 06:23:24.109860+00:00}
2026-04-08 06:23:24.109901+00:00],
2026-04-08 06:23:24.109929+00:00"model": "Perplexity Sonar",
2026-04-08 06:23:24.109953+00:00"usage": {
2026-04-08 06:23:24.109976+00:00"tokens": 2,
2026-04-08 06:23:24.109999+00:00"price": 0,
2026-04-08 06:23:24.110023+00:00"cost": 0
2026-04-08 06:23:24.110062+00:00}
2026-04-08 06:23:24.110085+00:00}
2026-04-08 06:23:24.110108+00:00],
2026-04-08 06:23:24.110130+00:00"modalities": [
2026-04-08 06:23:24.110154+00:00"text"
2026-04-08 06:23:24.110176+00:00],
2026-04-08 06:23:24.110236+00:00"metadata": {
2026-04-08 06:23:24.110259+00:00"tools": "none"
2026-04-08 06:23:24.110282+00:00},
2026-04-08 06:23:24.110304+00:00"stream": false
2026-04-08 06:23:24.110327+00:00}
2026-04-08 06:23:25.366572+00:00ERROR: Provider perplexity failed: unsupported operand type(s) for +=: 'int' and 'str'
2026-04-08 06:23:25.367530+00:00Traceback (most recent call last):
2026-04-08 06:23:25.367567+00:00File "/usr/local/lib/python3.11/site-packages/llms/main.py", line 1987, in g_chat_completion
2026-04-08 06:23:25.367591+00:00await filter_func(final_response, context)
2026-04-08 06:23:25.367614+00:00File "/usr/local/lib/python3.11/site-packages/llms/extensions/app/__init__.py", line 921, in chat_response
2026-04-08 06:23:25.367694+00:00total_costs += request.get("cost", 0) or 0
2026-04-08 06:23:25.367722+00:00TypeError: unsupported operand type(s) for +=: 'int' and 'str'
2026-04-08 06:23:25.367746+00:002026-04-08T06:23:25.367746180Z
2026-04-08 06:23:25.371193+00:00ERROR [app]: run_chat unsupported operand type(s) for +=: 'int' and 'str'
2026-04-08 06:23:25.371253+00:00Traceback (most recent call last):
2026-04-08 06:23:25.371277+00:00File "/usr/local/lib/python3.11/site-packages/llms/extensions/app/__init__.py", line 229, in run_chat
2026-04-08 06:23:25.371301+00:00await ctx.chat_completion(chat_req, context=context_req)
2026-04-08 06:23:25.371359+00:00File "/usr/local/lib/python3.11/site-packages/llms/main.py", line 3391, in chat_completion
2026-04-08 06:23:25.371383+00:00return await self.app.chat_completion(chat, context=context)
2026-04-08 06:23:25.371421+00:00^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-04-08 06:23:25.371445+00:00File "/usr/local/lib/python3.11/site-packages/llms/main.py", line 3069, in chat_completion
2026-04-08 06:23:25.371484+00:00response = await g_chat_completion(chat, context)
2026-04-08 06:23:25.371508+00:00^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2026-04-08 06:23:25.371531+00:00File "/usr/local/lib/python3.11/site-packages/llms/main.py", line 2004, in g_chat_completion
2026-04-08 06:23:25.371621+00:00raise first_exception
2026-04-08 06:23:25.371655+00:00File "/usr/local/lib/python3.11/site-packages/llms/main.py", line 1987, in g_chat_completion
2026-04-08 06:23:25.371690+00:00await filter_func(final_response, context)
2026-04-08 06:23:25.371724+00:00File "/usr/local/lib/python3.11/site-packages/llms/extensions/app/__init__.py", line 921, in chat_response
2026-04-08 06:23:25.371823+00:00total_costs += request.get("cost", 0) or 0
2026-04-08 06:23:25.371860+00:00TypeError: unsupported operand type(s) for +=: 'int' and 'str'
2026-04-08 06:23:25.371894+00:002026-04-08T06:23:25.371894194Z

Troubleshooting steps ive taken

  • ive tried setting the costs manually in the json file
  • tried moving the config to providers-extra.json
  • i went to cluade.al for help

what the AI said to do:

modify /usr/local/lib/python3.11/site-packages/llms/extensions/app/__init__.py or llms/llms/extensions/app/__init__.py lines 921-923

-                total_costs += request.get("cost", 0) or 0
-                total_input += request.get("inputTokens", 0) or 0
-                total_output += request.get("outputTokens", 0) or 0
+                try:
+                    _c = request.get("cost", 0) or 0
+                    if isinstance(_c, dict): total_costs += float(_c.get("total_cost", 0))
+                    elif isinstance(_c, str) and _c.startswith("{"): total_costs += float(__import__("json").loads(_c).get("total_cost", 0))
+                    elif _c: total_costs += float(_c)
+                except Exception: pass
+                total_input += int(float(request.get("inputTokens", 0) or 0))
+                total_output += int(float(request.get("outputTokens", 0) or 0))

Results

this did get rid of the error, however i dont know the code well enough to make PR and just dump AI code as a fix.
i did patch my local docker container and it seems fine but i dont know how that section of code is used by other models

comment

i am really new to this project and went digging through docs, and nothing really fit what if the model is in https://models.dev but not already listed in providers.json so i am totally open to the possibility that i did this adding of perplexity wrong or that im missing something

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions