Skip to content

[Conductor] Add Python client API - #4469

Merged
Icedcoco merged 2 commits into
kvcache-ai:mainfrom
Chase-Rong:split/conductor-python-api
Oct 12, 2026
Merged

Icedcoco merged 2 commits into
kvcache-ai:mainfrom
Chase-Rong:split/conductor-python-api

Conversation

@Chase-Rong

@Chase-Rong Chase-Rong commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Expose the existing Conductor RPC client through mooncake.conductor.ConductorClient, allowing Python applications to
register serving instances, query prefix-cache matches, and inspect Conductor state.

  • Add the _conductor pybind11 extension for setup, close, health checks, queries, registration, unregistration,
    service listing, and global view inspection.
  • Provide a Python wrapper with context-manager support, API docstrings, and error-code constants.
  • Validate registration dictionaries consistently, rejecting unknown keys, invalid field types, and integer overflow
    with ValueError.
  • Build and install the extension when WITH_CONDUCTOR=ON.
  • Include libasio.so in Conductor-only Python installations so the native extension can resolve its runtime
    dependency.
  • Add wrapper unit tests, registration validation tests, and real-server end-to-end tests.

Installation rules and argument validation are included here so the Python API is complete when introduced. Default
enablement and release-wheel integration are handled by #4470.

Related PRs and merge order

This is part 2 of the series split from #4457:

  1. [Conductor] Reuse Mooncake existing libzmq dependency #4501 (merged): unify Conductor's ZeroMQ dependency with Mooncake's existing libzmq infrastructure.
  2. [Conductor] Add Python client API #4469 (this PR): expose the Python client and its installation rules.
  3. [CI/Build] Ship and validate Conductor in release wheels #4470: enable, package, and validate Conductor in release wheels.

Blocks #4470. Remaining merge order: #4469 → #4470.

Together, these PRs replace the scope of #4457.

Module

  • Transfer Engine (mooncake-transfer-engine)
  • Mooncake Store (mooncake-store)
  • Mooncake Conductor (mooncake-conductor)
  • Reshard (mooncake-reshard)
  • Mooncake EP (mooncake-ep)
  • Mooncake PG (mooncake-pg)
  • Integration (mooncake-integration)
  • P2P Store (mooncake-p2p-store)
  • Python Wheel (mooncake-wheel)
  • Common (mooncake-common)
  • Mooncake RL (mooncake-rl)
  • CI/CD
  • Docs
  • Other

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Breaking change
  • Documentation update
  • Performance improvement
  • Other

How Has This Been Tested?

Built a new source wheel in a separate build directory with WITH_CONDUCTOR=ON, WITH_TE=OFF, WITH_STORE=OFF, USE_TENT=OFF, and USE_ETCD=OFF. Installed that wheel into a new virtual environment and used the server executable built from this same commit.

Test commands:

Paths are abbreviated below. INSTALLED_PYTHON is the new virtual environment's interpreter; CONDUCTOR_BINARY points to the newly built server. VERIFY_DIR contains the external validation helpers.

env -u PYTHONPATH -u PYTHONHOME -u LD_LIBRARY_PATH -u LD_PRELOAD \
  "$INSTALLED_PYTHON" -I -c \
  'import mooncake.conductor, mooncake._conductor'

"$INSTALLED_PYTHON" -I -m pytest -q \
  mooncake-conductor/tests/python/test_client_validation.py \
  "$VERIFY_DIR/test_api_e2e_installed.py" \
  "$EXTENDED_TESTS/test_extended_fixed.py"

"$INSTALLED_PYTHON" -I -m pytest -q \
  python/tests/unit/test_conductor_client.py \
  python/tests/packaging/test_project.py

pre-commit run --files $(git diff --name-only --diff-filter=ACMR HEAD^ HEAD)
./scripts/code_format.sh --check --changed-lines --base HEAD^
git diff --check HEAD^ HEAD

All installed-package tests ran with PYTHONPATH, PYTHONHOME, LD_LIBRARY_PATH, and LD_PRELOAD removed. The E2E helper preserves the repository's two original tests and replaces only their build-tree import/preload setup with an import from the installed wheel.

Test results:

  • Unit tests pass

  • Integration tests pass (if applicable)

  • Manual testing done (describe below)

  • Fresh Conductor-only wheel build, installation, and isolated import passed. The archive contains conductor.py, the CPython 3.13 _conductor extension, and libasio.so; imported module paths point into the new virtual environment.

  • 118 installed-package tests passed: 80 native registration validation cases, the two original real-server E2E cases adapted as described above, and 36 external regression cases.

  • The external regressions exercise real ZMQ event ingestion and prefix-cache hits, vLLM golden vectors, SGLang storage tiers, tenant/instance filtering, concurrent queries, registration/unregistration, timeout/error handling, and reconnect/lifecycle behavior. These helpers are validation artifacts, not additional tests committed in this PR.

  • 17 wrapper and project packaging tests passed. The wrapper unit tests use a stub native client; native behavior is covered separately by the installed-package tests above.

  • Client construction, uninitialized health status, and close passed in an isolated installed-package smoke check.

  • PR-scoped pre-commit, changed-line formatting, and whitespace checks passed on this branch.

The new wheel is a host Linux wheel, not a manylinux release artifact. Other Python ABIs, accelerator platforms, and hardware execution were not tested in this branch validation.

Checklist

  • I have performed a self-review of my own code
  • I have formatted my code using ./scripts/code_format.sh
  • I have run pre-commit on the files changed in this PR and all hooks pass
  • I have updated the documentation (if applicable)
  • I have added tests to prove my changes are effective
  • For changes >500 LOC: I have filed an RFC issue

PR-scoped pre-commit and changed-line formatting passed. API docstrings are included; no standalone documentation page is added.

The human self-review checkbox is left for the submitter to confirm. The change is below the RFC size threshold after excluding tests and configuration; no RFC issue is claimed.

AI Assistance Disclosure

  • No AI tools were used
  • AI tools were used (specify below)

@Chase-Rong Chase-Rong changed the title Split/conductor python api [Conductor] Add Python client API Oct 4, 2026
@Chase-Rong Chase-Rong added run-ci and removed run-ci labels Oct 8, 2026
@Chase-Rong
Chase-Rong force-pushed the split/conductor-python-api branch from 8d65bed to 171586f Compare October 9, 2026 07:18
@Chase-Rong
Chase-Rong requested review from Aionw and XucSh October 10, 2026 08:20

@Icedcoco Icedcoco left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for adding the Python client API. I think the overall direction is useful, and I had a few points that may be worth discussing before merging:

  1. In PyConductorClient::close(), setup_called_ remains true after the underlying client is closed. As a result, a subsequent health_check() may report HEALTH_UNREACHABLE rather than HEALTH_NOT_INITIALIZED, and a failed re-setup could leave the state indicating that setup had succeeded. Would it make sense to reset this flag in close() and update it consistently across setup attempts?

  2. conductor.py is installed unconditionally, while _conductor is only built when WITH_CONDUCTOR=ON. Users who build the package without conductor support will therefore have the Python module present, but importing mooncake.conductor will fail because the native extension is absent. This does not affect users who do not import the module, but could we consider making the installation conditional or providing a clearer optional-feature behavior?

  3. The Python conversion currently uses integer keys for dp and rank_matches, whereas the existing HTTP/msgpack response serializes rank keys as strings. Is the difference intentional? Keeping the shapes aligned might make it easier for clients to switch between the HTTP and Python APIs; alternatively, the distinction could be documented explicitly.

These points seem localized to the new conductor-facing API, and I did not see an obvious regression to the existing non-conductor functionality.

Reset initialization and replace the previous connection on every setup attempt. Explain missing optional Conductor support without masking extension loading errors.

Use string rank keys and expose device-memory cache hits as device in the Python API. Add lifecycle and HTTP equivalence regressions, and clarify API conventions in docstrings.
@Chase-Rong
Chase-Rong force-pushed the split/conductor-python-api branch from 8aaa2dd to 094d67f Compare October 12, 2026 07:36
@Chase-Rong

Copy link
Copy Markdown
Collaborator Author

Thanks for adding the Python client API. I think the overall direction is useful, and I had a few points that may be worth discussing before merging:

  1. In PyConductorClient::close(), setup_called_ remains true after the underlying client is closed. As a result, a subsequent health_check() may report HEALTH_UNREACHABLE rather than HEALTH_NOT_INITIALIZED, and a failed re-setup could leave the state indicating that setup had succeeded. Would it make sense to reset this flag in close() and update it consistently across setup attempts?
  2. conductor.py is installed unconditionally, while _conductor is only built when WITH_CONDUCTOR=ON. Users who build the package without conductor support will therefore have the Python module present, but importing mooncake.conductor will fail because the native extension is absent. This does not affect users who do not import the module, but could we consider making the installation conditional or providing a clearer optional-feature behavior?
  3. The Python conversion currently uses integer keys for dp and rank_matches, whereas the existing HTTP/msgpack response serializes rank keys as strings. Is the difference intentional? Keeping the shapes aligned might make it easier for clients to switch between the HTTP and Python APIs; alternatively, the distinction could be documented explicitly.

These points seem localized to the new conductor-facing API, and I did not see an obvious regression to the existing non-conductor functionality.

Thanks for the review. I have addressed the three points:

  1. Lifecycle state: close() now clears the initialization flag. Each setup() attempt replaces the previous
    connection, and failure leaves both the Python wrapper and underlying C++ client uninitialized. health_check()
    therefore returns HEALTH_NOT_INITIALIZED after close or failed setup, while an initialized client whose server
    becomes unreachable returns HEALTH_UNREACHABLE.

  2. Optional-feature behavior: The Python wrapper remains available. If mooncake._conductor is absent, importing
    mooncake.conductor raises an actionable ImportError suggesting a package with Conductor support or rebuilding with
    WITH_CONDUCTOR=ON. Other loading errors, such as missing shared libraries, retain their original exceptions.

  3. Rank keys: Both dp and rank_matches now use decimal string keys, matching the HTTP/msgpack API. Real-server
    tests verify ranks "0" and "12" and compare the returned values across both interfaces.

@UNIDY2002
UNIDY2002 removed their request for review October 12, 2026 07:42

@Icedcoco Icedcoco left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@Icedcoco
Icedcoco merged commit eac2344 into kvcache-ai:main Oct 12, 2026
25 checks passed
@whn09 whn09 mentioned this pull request Oct 12, 2026
7 of 32 tasks
@whn09

whn09 commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

Heads-up: #4469 and #4588 were merged about a minute apart, and together they break the build on main (8c9af6c9). #4469 added mooncake-integration/conductor/conductor_py.cpp, which reads m.npu and hit.npu. #4588 renamed those fields to device in RankCacheHitResult / CacheHitResult. Each PR's CI passed on its own base.

From the CTest unit tests job on current main-based PRs (e.g. #4608, log):

mooncake-integration/conductor/conductor_py.cpp:25:26: error: '...RankCacheHitResult' has no member named 'npu'
mooncake-integration/conductor/conductor_py.cpp:32:23: error: 'const struct mooncake::conductor::prefixindex::CacheHitResult' has no member named 'npu'

The fix looks like a two-line change in conductor_py.cpp: m.npu -> m.device (line 25) and hit.npu -> hit.device (line 32). The Python-facing key is already "device", so the API does not change. Until this lands, every PR rebased on current main fails CTest unit tests. @Chase-Rong @Misak2333, could one of you push the fix? Happy to send it if you prefer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants