Skip to content

fix: extract entities tool_calls some times is an array #2481

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

Merged
merged 1 commit into from
Apr 9, 2025

Conversation

sergio-toro
Copy link
Contributor

Description

When passing big prompts or history of conversations, sometimes, tools_calls response for extract_entities is an array. This fixes generating a big graph for given history of chat interactions between user & assistant.

Memories add request that some times fails:

{
  "messages": [
    {"role": "user", "content": "I started yoga classes yesterday."},
    {"role": "assistant", "content": "That's wonderful, Sarah! How did you feel after your first yoga session?"},
    {"role": "user", "content": "It was relaxing, but a bit challenging."},
    {"role": "assistant", "content": "I'll remember that yoga was relaxing yet challenging for you, Sarah. Consistency will make it easier with time."},

    {"role": "user", "content": "I'm feeling anxious about my presentation next week."},
    {"role": "assistant", "content": "I'm sorry to hear that, John. Do you want to talk through some strategies to manage that anxiety?"},

    {"role": "user", "content": "My sister just had a baby, I'm an aunt now!"},
    {"role": "assistant", "content": "Congratulations, Emily! Becoming an aunt is a joyful experience. How do you feel about this new role?"},

    {"role": "user", "content": "I'm planning to run a half-marathon in April."},
    {"role": "assistant", "content": "That's a fantastic goal, Mike! Would you like me to help you create a training schedule?"},

    {"role": "user", "content": "I'm going through a tough breakup right now."},
    {"role": "assistant", "content": "I'm sorry you're going through this difficult time, Laura. I'm here to support you—would you like to talk more about your feelings?"},

    {"role": "user", "content": "I visited Italy last summer and fell in love with the food."},
    {"role": "assistant", "content": "Italy does have amazing food, Ben! Any favorite dish you particularly enjoyed?"},

    {"role": "user", "content": "I've been experiencing insomnia lately."},
    {"role": "assistant", "content": "That must be challenging, Anna. Would you like to explore some relaxation techniques that might help?"},

    {"role": "user", "content": "I just adopted a puppy!"},
    {"role": "assistant", "content": "How exciting, Olivia! Puppies can bring so much joy. Have you named your new companion yet?"},

    {"role": "user", "content": "My grandfather passed away last week."},
    {"role": "assistant", "content": "I'm very sorry for your loss, David. If you'd like to share some memories or talk about how you're feeling, I'm here for you."},

    {"role": "user", "content": "I want to learn how to cook healthier meals."},
    {"role": "assistant", "content": "That's a great idea, Emma! Would you like me to suggest some easy, nutritious recipes to get started?"},

    {"role": "user", "content": "I felt overwhelmed at work today."},
    {"role": "assistant", "content": "That sounds tough, Liam. Would you like to discuss strategies to help manage stress at work?"},

    {"role": "user", "content": "I'm excited about moving to a new apartment next month."},
    {"role": "assistant", "content": "That's exciting news, Mia! Are you looking forward to decorating your new space?"},

    {"role": "user", "content": "I tried meditation for the first time yesterday."},
    {"role": "assistant", "content": "That's a positive step, Lucas! How did you feel after meditating?"},

    {"role": "user", "content": "I've been feeling lonely lately."},
    {"role": "assistant", "content": "I'm here for you, Sophie. Would you like to talk more about what's making you feel lonely?"},

    {"role": "user", "content": "I'm training for a triathlon this summer."},
    {"role": "assistant", "content": "That's an impressive goal, Ryan! Do you need help planning your training routine?"},

    {"role": "user", "content": "I had a wonderful time at my friend's wedding last weekend."},
    {"role": "assistant", "content": "That's lovely to hear, Chloe! Weddings can be such happy events. Did you enjoy the celebration?"}
  ],
  "user_id": "uuid-us3r-1",
  "agent_id": "agent-oli",
  "run_id": "chat1",
  "metadata": {}
}

Response from LLM:

{
  'content': None,
  'tool_calls': [
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'yoga classes',
            'entity_type': 'Event'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'presentation',
            'entity_type': 'Event'
          },
          {
            'entity': 'next week',
            'entity_type': 'Date'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'half-marathon',
            'entity_type': 'Event'
          },
          {
            'entity': 'April',
            'entity_type': 'Date'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'breakup',
            'entity_type': 'Event'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'Italy',
            'entity_type': 'Location'
          },
          {
            'entity': 'last summer',
            'entity_type': 'Date'
          },
          {
            'entity': 'food',
            'entity_type': 'Thing'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'insomnia',
            'entity_type': 'Condition'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'healthier meals',
            'entity_type': 'Thing'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'work',
            'entity_type': 'Place'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'new apartment',
            'entity_type': 'Place'
          },
          {
            'entity': 'next month',
            'entity_type': 'Date'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'meditation',
            'entity_type': 'Activity'
          },
          {
            'entity': 'yesterday',
            'entity_type': 'Date'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'lonely',
            'entity_type': 'Feeling'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': 'triathlon',
            'entity_type': 'Event'
          },
          {
            'entity': 'this summer',
            'entity_type': 'Date'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'uuid-us3r-1',
            'entity_type': 'Person'
          },
          {
            'entity': "friend's wedding",
            'entity_type': 'Event'
          },
          {
            'entity': 'last weekend',
            'entity_type': 'Date'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Sarah',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'John',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Emily',
            'entity_type': 'Person'
          },
          {
            'entity': 'sister',
            'entity_type': 'Person'
          },
          {
            'entity': 'baby',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Mike',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Laura',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Ben',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Anna',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Olivia',
            'entity_type': 'Person'
          },
          {
            'entity': 'puppy',
            'entity_type': 'Animal'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'David',
            'entity_type': 'Person'
          },
          {
            'entity': 'grandfather',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Emma',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Liam',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Mia',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Lucas',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Sophie',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Ryan',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Chloe',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Sarah',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'John',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Emily',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Mike',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Laura',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Ben',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Anna',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Olivia',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'David',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Emma',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Liam',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Mia',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Lucas',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Sophie',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Ryan',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Chloe',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Sarah',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'John',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Emily',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Mike',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Laura',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Ben',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Anna',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Olivia',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'David',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Emma',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Liam',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Mia',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Lucas',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Sophie',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Ryan',
            'entity_type': 'Person'
          }
        ]
      }
    },
    {
      'name': 'extract_entities',
      'arguments': {
        'entities': [
          {
            'entity': 'Chloe',
            'entity_type': 'Person'
          }
        ]
      }
    }
  ]
}

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

Please delete options that are not relevant.

  • Unit Test
  • Test Script (please provide)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Maintainer Checklist

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Made sure Checks passed

for item in search_results["tool_calls"][0]["arguments"]["entities"]:
entity_type_map[item["entity"]] = item["entity_type"]
for tool_call in search_results["tool_calls"]:
if tool_call['name'] != "extract_entities":
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain the fix? Because I can name: extract_entities being present in all the records in the example you mentioned in the description.

I'm a little out of place here can you elaborate more?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fix is that it iterates over the 2 levels of arrays.

It is doing exactly the same as your Typescript version of the code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if (call.name === "extract_entities") {

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it, Thanks!

@Dev-Khant Dev-Khant merged commit 55df395 into mem0ai:main Apr 9, 2025
5 of 8 checks passed
rohankatakam pushed a commit to jean-technologies/jean-memory that referenced this pull request Jun 22, 2025
merlinfrombelgium pushed a commit to merlinfrombelgium/mem0 that referenced this pull request Jul 4, 2025
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