Skip to content

[Bug]:Duplicate Entry Error in AsyncSQLAlchemyMemory with parallel_tool_calls=True #1381

@define9527

Description

@define9527

AgentScope is an open-source project. To involve a broader community, we recommend asking your questions in English.

Describe the bug

When using ReActAgent with parallel_tool_calls=True and AsyncSQLAlchemyMemory, I'm encountering a database integrity error when multiple tool calls execute concurrently. The error occurs because multiple _acting coroutines call memory.add() simultaneously, resulting in duplicate primary key conflicts.

To Reproduce

  1. Create a ReActAgent with parallel_tool_calls=True
  2. Configure it to use AsyncSQLAlchemyMemory

Expected behavior

Solve this problem

Error messages

sqlalchemy.exc.IntegrityError: (pymysql.err.IntegrityError) (1062, "Duplicate entry 'user_913e9801-user_913e9801_itinerary-dEvmWpbijBSmoA84LoK4RY' for key 'PRIMARY'")
[SQL: INSERT INTO message (id, msg, session_id, `index`) VALUES (%s, %s, %s, %s)]
[parameters: [('user_913e9801-user_913e9801_itinerary-dEvmWpbijBSmoA84LoK4RY', '{"id": "dEvmWpbijBSmoA84LoK4RY", "name": "system", "role": "system", "content": [{"type": "tool_result", "id": "call_ab693550cc4b4499b74d47", "name": ... (9 characters truncated) ... light", "output": [{"type": "text", "text": "\\u67e5\\u8be2\\u822a\\u73ed\\u6210\\u529f"}]}], "metadata": {}, "timestamp": "2026-03-28 16:05:41.744"}', 'user_913e9801_itinerary', 9), ('user_913e9801-user_913e9801_itinerary-3k9tYqZvmze59zZJ4GEnHC', '{"id": "3k9tYqZvmze59zZJ4GEnHC", "name": "system", "role": "system", "content": [{"type": "tool_result", "id": "call_3f95f8ff924e43aaaaccc0", "name": ... (1 characters truncated) ... "plan", "output": [{"type": "text", "text": "\\u89c4\\u5212\\u884c\\u7a0b\\u6210\\u529f"}]}], "metadata": {}, "timestamp": "2026-03-28 16:05:41.752"}', 'user_913e9801_itinerary', 9)]]
(Background on this error at: https://sqlalche.me/e/20/gkpj)

Environment (please complete the following information):

  • agentscope==1.0.16
  • fastapi[standard]==0.135.1
  • uvicorn[standard]==0.41.0
  • pydantic==2.12.5
  • aiomysql==0.3.2
  • pyyaml==6.0.1
  • qdrant-client==1.17.0
  • Python Version: [e.g. 3.12]
  • OS: [e.g. windows]

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions