AttributeError: 'BaseMessageParam' Object Has No Attribute 'get'
AttributeError: 'BaseMessageParam' object has no attribute 'get' in Mirascope v1.21.0+
In Mirascope v1.21.0 and later versions, a critical issue arises when a BaseMessageParam
object is treated as a dictionary with the get
method. However, this object does not possess such a method, resulting in an AttributeError
when utilizing Mirascope's LLM functionality directly or through integration with Lilypad.
To replicate this issue, follow these steps:
- Install Mirascope v1.21.0 or newer: Ensure you have the latest version of Mirascope installed.
- Set up the appropriate API keys: Configure the necessary API keys for Mirascope.
- Run the minimal example: Execute the following code snippet:
from mirascope import llm
@llm.call("openai","gpt-4o-mini")
def generate_text(prompt: str) -> str:
return prompt
response = generate_text("Hello, world!")
print(response.common_messages)
When running the above code, you will encounter the following error:
AttributeError: 'BaseMessageParam' object has no attribute 'get'
File ".../mirascope/core/openai/_utils/_message_param_converter.py", line 41, in from_provider
content = message_param.get("content")
The complete error traceback is as follows:
Traceback (most recent call last):
File "/Users/koudai/Library/Application Support/JetBrains/PyCharm2024.3/scratches/scratch_214.py", line 8, in <module>
print(response.common_messages)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/koudai/PycharmProjects/lilypad/.venv/lib/python3.12/site-packages/pydantic/main.py", line 888, in __getattr__
return super().__getattribute__(item) # Raises AttributeError if appropriate
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/koudai/PycharmProjects/lilypad/.venv/lib/python3.12/site-packages/mirascope/core/base/call_response.py", line 324, in common_messages
return self._message_converter.from_provider(self.messages)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/koudai/PycharmProjects/lilypad/.venv/lib/python3.12/site-packages/mirascope/core/openai/_utils/_message_param_converter.py", line 41, in from_provider
content = message_param.get("content")
^^^^^^^^^^^^^^^^^
File "/Users/koudai/PycharmProjects/lilypad/.venv/lib/python3.12/site-packages/pydantic/main.py", line 891, in __getattr__
raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'BaseMessageParam' object has no attribute 'get'
Upon executing the code, you will notice that:
- The API call succeeds and returns a response.
- However, when attempting to access
common_messages
or convert messages, an error is thrown becauseBaseMessageParam
is being treated as a dictionary when it's not.
The code should run without errors and properly process the response messages.
- Mirascope: v1.21.0
- Python 3.12
- This issue also affects integration with Lilypad.
The error appears to be in the _message_param_converter.py
file, where it's trying to use dictionary-style access on a BaseMessageParam
object. This likely needs to be updated to use proper object attribute access instead of dictionary-style access.
This bug affects both direct usage of Mirascope and integration with other libraries like Lilypad.
Python, Mirascope & OS Versions, related packages (not required)
# Python version
python = "3.12"
# Mirascope version
mirascope = "1.21.0"
# OS version
os = "macOS 12.6.3"
# Related packages
packages = ["pydantic", "mirascope", "lilypad"]
To resolve this issue, you can update the _message_param_converter.py
file to use proper object attribute access instead of dictionary-style access. This will ensure that the BaseMessageParam
object is treated correctly, and the code will run without errors.
Q&A: AttributeError: 'BaseMessageParam' object has no attribute 'get' in Mirascope v1.21.0+
A: In Mirascope v1.21.0 and later versions, a BaseMessageParam
object is being treated as a dictionary with the get
method, but this object does not possess such a method. This results in an AttributeError
when utilizing Mirascope's LLM functionality directly or through integration with Lilypad.
A: To replicate this issue, follow these steps:
- Install Mirascope v1.21.0 or newer.
- Set up the appropriate API keys.
- Run the minimal example:
from mirascope import llm
@llm.call("openai","gpt-4o-mini")
def generate_text(prompt: str) -> str:
return prompt
response = generate_text("Hello, world!")
print(response.common_messages)
A: When running the above code, you will encounter the following error:
AttributeError: 'BaseMessageParam' object has no attribute 'get'
File ".../mirascope/core/openai/_utils/_message_param_converter.py", line 41, in from_provider
content = message_param.get("content")
A: The complete error traceback is as follows:
Traceback (most recent call last):
File "/Users/koudai/Library/Application Support/JetBrains/PyCharm2024.3/scratches/scratch_214.py", line 8, in <module>
print(response.common_messages)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/koudai/PycharmProjects/lilypad/.venv/lib/python3.12/site-packages/pydantic/main.py", line 888, in __getattr__
return super().__getattribute__(item) # Raises AttributeError if appropriate
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/koudai/PycharmProjects/lilypad/.venv/lib/python3.12/site-packages/mirascope/core/base/call_response.py", line 324, in common_messages
return self._message_converter.from_provider(self.messages)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/koudai/PycharmProjects/lilypad/.venv/lib/python3.12/site-packages/mirascope/core/openai/_utils/_message_param_converter.py", line 41, in from_provider
content = message_param.get("content")
^^^^^^^^^^^^^^^^^
File "/Users/koudai/PycharmProjects/lilypad/.venv/lib/python3.12/site-packages/pydantic/main.py", line 891, in __getattr__
raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'BaseMessageParam' object has no attribute 'get'
A: Upon executing the code, you will notice that:
- The API call succeeds and returns a response.
- However, when attempting to access
common_messages
or convert messages, an error is thrown becauseBaseMessageParam
is being treated as a dictionary when it's not.
A: The code should run without errors and properly process the response messages.
A: The environment details are as follows:
- Mirascope: v1.21.0
- Python 3.12
- This issue also affects integration with Lilypad.
A: The error appears to be in the _message_param_converter.py
file, where it's trying to use dictionary-style access on a BaseMessageParam
object. This likely needs to be updated to use proper object attribute access instead of dictionary-style access.
A: To resolve this issue, you can update the _message_param_converter.py
file to use proper object attribute access instead of dictionary-style access. This will ensure that the BaseMessageParam
object is treated correctly, and the code will run without errors.