Skip to content

vertexai: handle messages that evaluate to a literal float #938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

JoshuaPostel
Copy link

PR Description

the function _convert_to_parts(message: BaseMessage) throws an error when the message passed gets evaluated to a float. Example error:

...

  File "/XXX/.venv/lib/python3.10/site-packages/langchain_google_vertexai/chat_models.py", line 313, in _parse_chat_history_gemini                                             
    parts = _convert_to_parts(message)                                                                                                                                                                             
            │                 └ HumanMessage(content='3.5', additional_kwargs={}, response_metadata={})                                                                                                            
            └ <function _parse_chat_history_gemini.<locals>._convert_to_parts at 0x167677d90>                                                                                                                      
  File "/XXX/.venv/lib/python3.10/site-packages/langchain_google_vertexai/chat_models.py", line 280, in _convert_to_parts                                                      
    for raw_part in raw_content:                                                                                                                                                                                   
                    └ 3.5   

TypeError: 'float' object is not iterable

Relevant issues

None

Type

🐛 Bug Fix

Copy link
Contributor

@SYED-M-HUSSAIN SYED-M-HUSSAIN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if isinstance(raw_content, (int, float, str)):
    raw_content = [str(raw_content)]
elif isinstance(raw_content, list):
    raw_content = [str(item) for item in raw_content]
else:
    raise TypeError(f"Unsupported type: {type(raw_content)}")

This version merges the first two conditions into one, as both involve converting the value to a string and wrapping it in a list.

@JoshuaPostel
Copy link
Author

@SYED-M-HUSSAIN are you requesting I apply these changes to this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants