Skip to content

Python: unhashable type exception for custom return type in kernel function #10299

Closed

Description

Describe the bug

TypeError: unhashable type: 'AccountBalance'

I am using a kernel function that returns a custom type:

    @kernel_function(description="Get user account balance")
    async def get_user_account_balance(self, account_id: str) -> "AccountBalance":
        //....
        return AccountBalance(account_balance=account_balance)

    class AccountBalance(BaseModel):
          balance: int = Field(..., alias="account_balance")

To Reproduce
Steps to reproduce the behavior:

  1. Create an agent group chat
  2. Add an agent to the group chat with a kernel function that returns a custom type such as AccountBalance
  3. Invoke the agent through the group chat and let the agent invoke the kernel function that returns a list
  4. See error

Expected behavior
Custom types in kernel function should work

Screenshots
If applicable, add screenshots to help explain your problem.

Platform

  • Language: Python
  • Source: 1.19.0
  • IDE: VS Code]
  • OS: Mac

Additional context

  File "/demo/.venv/lib/python3.12/site-packages/semantic_kernel/agents/group_chat/agent_group_chat.py", line 144, in invoke
    async for message in super().invoke_agent(selected_agent):
  File "/demo/.venv/lib/python3.12/site-packages/semantic_kernel/agents/group_chat/agent_chat.py", line 144, in invoke_agent
    async for is_visible, message in channel.invoke(agent):
  File "/demo/.venv/lib/python3.12/site-packages/semantic_kernel/agents/channels/chat_history_channel.py", line 75, in invoke
    mutated_history.add(mutated_message)
  File "/demo/.venv/lib/python3.12/site-packages/semantic_kernel/contents/chat_message_content.py", line 318, in __hash__
    return hash((self.tag, self.role, self.content, self.encoding, self.finish_reason, *self.items))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/demo/.venv/lib/python3.12/site-packages/semantic_kernel/contents/function_result_content.py", line 197, in __hash__
    return hash((
           ^^^^^^
TypeError: unhashable type: 'AccountBalance'

Activity

  1. moonbox3 commented on Jan 28, 2025

    @moonbox3
    Collaborator

    Hi Abhishek Gupta (@abhirockzz), we'll improve the handling for types so the hash doesn't error. To unblock you right now, I'd advise that you make you freeze your BaseModel like:

    class AccountBalance(BaseModel):
        # Make the model frozen so it's hashable
        balance: int = Field(..., alias="account_balance")
        model_config = ConfigDict(frozen=True)

    I tested this with:

    def test_hash_with_frozen_account_balance():
        balance = AccountBalance(account_balance=100)
        content = FunctionResultContent(
            id="test_id",
            result=balance,
            function_name="TestFunction",
        )
        # Verify we can call hash without error
        _ = hash(content)
        assert True, "Hashing FunctionResultContent with frozen model should not raise errors."

    and this passes. That would be the quickest way to handle it now while we make some improvements. Thanks for filing the issue.

  2. added a commit that references this issue on Jan 28, 2025
    7e20631
  3. added a commit that references this issue on Sep 15, 2025
    f61714c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workingpythonPull requests for the Python Semantic Kernel

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions