Skip to content

feat(utils): add configurable ID factory via set_id_factory()#1839

Open
lajxw wants to merge 6 commits into
agentscope-ai:mainfrom
lajxw:main
Open

feat(utils): add configurable ID factory via set_id_factory()#1839
lajxw wants to merge 6 commits into
agentscope-ai:mainfrom
lajxw:main

Conversation

@lajxw

@lajxw lajxw commented Jun 9, 2026

Copy link
Copy Markdown

AgentScope Version

2.0.1

Description

Background: AgentScope hardcodes uuid.uuid4().hex for all entity IDs. This prevents alignment with production databases that use other ID strategies such as UUIDv7 (time-ordered), causing block_id mismatch between streaming events and persisted records.

Changes:
- Added _id_factory global and set_id_factory() in src/agentscope/_utils/_common.py, allowing users to override the default ID generation strategy at startup.
- Exported set_id_factory from src/agentscope/init.py as public API.
- Replaced 34 occurrences of uuid.uuid4().hex with _generate_id / _generate_id() across 18 entity files.
- Security-sensitive tokens (gateway tokens, Redis lock tokens) intentionally remain as uuid.uuid4().hex.

Close #1782

Checklist

Please check the following items before code is ready to be reviewed.

  • An issue has been created for this PR
  • I have read the CONTRIBUTING.md
  • Docstrings are in Google style
  • Related documentation has been updated (e.g. links, examples, etc.) in documentation repository
  • Code is ready for review

@DavdGao DavdGao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR. Two suggestions on the test file:

  1. Make the tests async to match our template. Please follow tests/test_template.py, which uses IsolatedAsyncioTestCase with asyncSetUp / asyncTearDown instead of the sync unittest.TestCase.
  2. Trim the test cases. A few of the current tests add little value (e.g. asserting the public export is the internal function, the non-callable TypeError check). Please keep just the two that capture the intent:
  • the default id_factory still returns uuid.uuid4().hex
  • after from agentscope import set_id_factory, a custom factory actually takes effect on entities

Comment thread tests/id_factory_test.py Outdated
@DavdGao

DavdGao commented Jun 12, 2026

Copy link
Copy Markdown
Member

@lajxw Plz format the code by running pre-commit run --all-files

@lajxw lajxw requested a review from DavdGao June 12, 2026 06:00
@lajxw

lajxw commented Jun 12, 2026

Copy link
Copy Markdown
Author

I have made the requested changes; please review again.

@DavdGao DavdGao left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread src/agentscope/_utils/_common.py
@lajxw lajxw requested a review from DavdGao June 15, 2026 01:51
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.

[Feature]: Allow customizable ID generator

2 participants