Skip to content

Cohere usage is broken #2495

@samuelcolvin

Description

@samuelcolvin

Initial Checks

Description

I get:

\Traceback (most recent call last):
  File "/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/pydantic_ai/models/cohere.py", line 37, in <module>
    from cohere import (
    ...<12 lines>...
    )
ImportError: cannot import name 'TextAssistantMessageContentItem' from 'cohere' (/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/cohere/__init__.py). Did you mean: 'TextAssistantMessageV2ContentItem'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/samuel/code/genai-prices/ex.py", line 9, in <module>
    agent = Agent(
        'cohere:command-r',
        system_prompt='Be concise, reply with one sentence.',
    )
  File "/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/pydantic_ai/agent.py", line 290, in __init__
    self.model = models.infer_model(model)
                 ~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/pydantic_ai/models/__init__.py", line 590, in infer_model
    from .cohere import CohereModel
  File "/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/pydantic_ai/models/cohere.py", line 54, in <module>
    raise ImportError(
    ...<2 lines>...
    ) from _import_error
ImportError: Please install `cohere` to use the Cohere model, you can use the `cohere` optional group — `pip install "pydantic-ai-slim[cohere]"`
code/genai-prices 1 ➤ uv run ex.py
Traceback (most recent call last):
  File "/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/pydantic_ai/models/cohere.py", line 37, in <module>
    from cohere import (
    ...<12 lines>...
    )
ImportError: cannot import name 'TextAssistantMessageContentItem' from 'cohere' (/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/cohere/__init__.py). Did you mean: 'TextAssistantMessageV2ContentItem'?

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/samuel/code/genai-prices/ex.py", line 9, in <module>
    agent = Agent(
        'cohere:command-r',
        system_prompt='Be concise, reply with one sentence.',
    )
  File "/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/pydantic_ai/agent.py", line 290, in __init__
    self.model = models.infer_model(model)
                 ~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/pydantic_ai/models/__init__.py", line 590, in infer_model
    from .cohere import CohereModel
  File "/Users/samuel/Library/Caches/uv/environments-v2/ex-505e6d66b20779a5/lib/python3.13/site-packages/pydantic_ai/models/cohere.py", line 54, in <module>
    raise ImportError(
    ...<2 lines>...
    ) from _import_error
ImportError: Please install `cohere` to use the Cohere model, you can use the `cohere` optional group — `pip install "pydantic-ai-slim[cohere]"`

looks like their api changed.

Example Code

# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "pydantic-ai-slim[cohere]",
# ]
# ///
from pydantic_ai import Agent

agent = Agent(
    'cohere:command-r',
    system_prompt='Be concise, reply with one sentence.',
)

result = agent.run_sync('Where does "hello world" come from?')
print(result.output)

Python, Pydantic AI & LLM client version

latest

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