Skip to content

Add doc for Python API - #3627

Merged
csukuangfj merged 2 commits into
k2-fsa:masterfrom
csukuangfj:python-api-doc
May 27, 2026
Merged

csukuangfj merged 2 commits into
k2-fsa:masterfrom
csukuangfj:python-api-doc

Conversation

@csukuangfj

@csukuangfj csukuangfj commented May 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Documentation
    • Enhanced Python API docs across many audio processing modules with detailed class, method, and property docstrings.
    • Added richer usage examples, parameter/return descriptions, and improved module-level guidance for recognizers, TTS, denoisers, diarization, keyword spotting, VAD, clustering, embedding, and related wrappers.
    • Exposed module metadata and clarified streaming/offline API behaviors in docs without changing runtime behavior.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 20, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1d4025cd-ac42-4b31-8478-a5420d3e15ce

📥 Commits

Reviewing files that changed from the base of the PR and between 1ed6e7b and 8a14d6c.

📒 Files selected for processing (4)
  • sherpa-onnx/python/csrc/keyword-spotter.cc
  • sherpa-onnx/python/csrc/offline-speaker-diarization.cc
  • sherpa-onnx/python/csrc/online-recognizer.cc
  • sherpa-onnx/python/sherpa_onnx/online_recognizer.py
✅ Files skipped from review due to trivial changes (2)
  • sherpa-onnx/python/sherpa_onnx/online_recognizer.py
  • sherpa-onnx/python/csrc/online-recognizer.cc

📝 Walkthrough

Walkthrough

Adds file-local docstring constants and attaches them to pybind11 registrations across many C++ modules; also expands Python facade class/module/method docstrings. No runtime behavior or public API signatures were changed.

Changes

Python API Documentation

Layer / File(s) Summary
C++ pybind docstring constants and bindings
sherpa-onnx/python/csrc/audio-tagging.cc, sherpa-onnx/python/csrc/circular-buffer.cc, sherpa-onnx/python/csrc/display.cc, sherpa-onnx/python/csrc/fast-clustering.cc, sherpa-onnx/python/csrc/keyword-spotter.cc, sherpa-onnx/python/csrc/offline-model-config.cc, sherpa-onnx/python/csrc/offline-punctuation.cc, sherpa-onnx/python/csrc/offline-recognizer.cc, sherpa-onnx/python/csrc/offline-source-separation.cc, sherpa-onnx/python/csrc/offline-speaker-diarization.cc, sherpa-onnx/python/csrc/offline-speech-denoiser.cc, sherpa-onnx/python/csrc/offline-stream.cc, sherpa-onnx/python/csrc/offline-tts.cc, sherpa-onnx/python/csrc/online-model-config.cc, sherpa-onnx/python/csrc/online-punctuation.cc, sherpa-onnx/python/csrc/online-recognizer.cc, sherpa-onnx/python/csrc/online-speech-denoiser.cc, sherpa-onnx/python/csrc/online-stream.cc, sherpa-onnx/python/csrc/speaker-embedding-extractor.cc, sherpa-onnx/python/csrc/speaker-embedding-manager.cc, sherpa-onnx/python/csrc/spoken-language-identification.cc, sherpa-onnx/python/csrc/version.cc, sherpa-onnx/python/csrc/voice-activity-detector.cc, sherpa-onnx/python/csrc/wave-writer.cc
Adds static constexpr raw-string doc constants and wires them into pybind11 .def / .def_property_readonly bindings to provide Python help text while preserving all bindings and GIL-release guards.
Python API-level class and method documentation
sherpa-onnx/python/sherpa_onnx/display.py, sherpa-onnx/python/sherpa_onnx/keyword_spotter.py, sherpa-onnx/python/sherpa_onnx/offline_recognizer.py, sherpa-onnx/python/sherpa_onnx/online_recognizer.py
Expands module/class/method docstrings and usage examples for public Python facade classes; implementation logic and public signatures remain unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

size:L, type:docs, area:python-bindings

Poem

🐰 A thousand docstrings now bloom fair,
Each method's purpose crystal clear,
From C++ bindings to Python air,
Help pops up when developers peer.
Quiet change — just words took flight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.21% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title 'Add doc for Python API' directly and clearly describes the primary change across all modified files—comprehensive Python docstrings were added throughout the codebase's C++ bindings and Python modules.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dosubot dosubot Bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label May 20, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@sherpa-onnx/python/csrc/keyword-spotter.cc`:
- Around line 58-67: The docstring constant kCreateStreamDoc documents a
`keywords` parameter but is currently attached to the zero-argument overload of
create_stream, producing incorrect Python help; update the binding so the
docstring matches the correct overload or revise kCreateStreamDoc to describe
the zero-arg create_stream signature. Locate the create_stream overload bindings
in keyword-spotter.cc and either (a) move/apply kCreateStreamDoc to the overload
that accepts the keywords parameter, or (b) change kCreateStreamDoc text to
remove the `keywords` section so it accurately documents the no-argument
create_stream overload referenced by the binding.

In `@sherpa-onnx/python/csrc/offline-speaker-diarization.cc`:
- Around line 43-45: The docstring promises that a non-zero return from the
progress callback will abort processing but the progress-callback wrapper in
offline-speaker-diarization.cc always returns 0; fix this by propagating the
callback's return value: change the wrapper that invokes
callback(processed_chunks, num_chunks) to return the callback's int result (and
ensure the caller checks for non-zero and aborts), or if you prefer not to
support aborting, update the docstring to remove the "non-zero abort" claim;
reference the callback symbol ("callback(processed_chunks, num_chunks)") and the
progress-callback wrapper in offline-speaker-diarization.cc when making the
change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cadb656b-c483-48f8-8e24-f7f6a5dbaa1b

📥 Commits

Reviewing files that changed from the base of the PR and between a703cf6 and 1ed6e7b.

📒 Files selected for processing (28)
  • sherpa-onnx/python/csrc/audio-tagging.cc
  • sherpa-onnx/python/csrc/circular-buffer.cc
  • sherpa-onnx/python/csrc/display.cc
  • sherpa-onnx/python/csrc/fast-clustering.cc
  • sherpa-onnx/python/csrc/keyword-spotter.cc
  • sherpa-onnx/python/csrc/offline-model-config.cc
  • sherpa-onnx/python/csrc/offline-punctuation.cc
  • sherpa-onnx/python/csrc/offline-recognizer.cc
  • sherpa-onnx/python/csrc/offline-source-separation.cc
  • sherpa-onnx/python/csrc/offline-speaker-diarization.cc
  • sherpa-onnx/python/csrc/offline-speech-denoiser.cc
  • sherpa-onnx/python/csrc/offline-stream.cc
  • sherpa-onnx/python/csrc/offline-tts.cc
  • sherpa-onnx/python/csrc/online-model-config.cc
  • sherpa-onnx/python/csrc/online-punctuation.cc
  • sherpa-onnx/python/csrc/online-recognizer.cc
  • sherpa-onnx/python/csrc/online-speech-denoiser.cc
  • sherpa-onnx/python/csrc/online-stream.cc
  • sherpa-onnx/python/csrc/speaker-embedding-extractor.cc
  • sherpa-onnx/python/csrc/speaker-embedding-manager.cc
  • sherpa-onnx/python/csrc/spoken-language-identification.cc
  • sherpa-onnx/python/csrc/version.cc
  • sherpa-onnx/python/csrc/voice-activity-detector.cc
  • sherpa-onnx/python/csrc/wave-writer.cc
  • sherpa-onnx/python/sherpa_onnx/display.py
  • sherpa-onnx/python/sherpa_onnx/keyword_spotter.py
  • sherpa-onnx/python/sherpa_onnx/offline_recognizer.py
  • sherpa-onnx/python/sherpa_onnx/online_recognizer.py

Comment thread sherpa-onnx/python/csrc/keyword-spotter.cc
Comment thread sherpa-onnx/python/csrc/offline-speaker-diarization.cc

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request significantly improves the documentation for the Python API by adding comprehensive docstrings and usage examples to both the C++ bindings and Python wrapper classes across numerous modules, including speech recognition, speaker diarization, and audio tagging. Feedback from the review identifies several issues in the newly added documentation, specifically highlighting incorrect code examples in online_recognizer.py that would cause attribute errors and mismatched docstrings for overloaded methods in the OnlineRecognizer and KeywordSpotter C++ bindings.

Comment on lines +75 to +76
result = recognizer.get_result(stream)
print(result.text)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The example code is incorrect because recognizer.get_result(stream) returns a str (as defined in line 1104), which does not have a .text attribute. To access the result object and its properties, recognizer.get_result_all(stream) should be used instead.

Suggested change
result = recognizer.get_result(stream)
print(result.text)
result = recognizer.get_result_all(stream)
print(result.text)

Comment on lines +87 to +88
result = recognizer.get_result(stream)
print("Endpoint detected:", result.text)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Same as the previous issue: recognizer.get_result(stream) returns a string, so accessing .text on it will raise an AttributeError. Use get_result_all(stream) to obtain the result object.

Suggested change
result = recognizer.get_result(stream)
print("Endpoint detected:", result.text)
result = recognizer.get_result_all(stream)
print("Endpoint detected:", result.text)

Comment on lines +65 to +74
static constexpr const char *kCreateStreamDoc = R"doc(
Create a new ``OnlineStream`` for decoding.

Args:
hotwords:
Optional hotwords for this stream. If provided, it is a string of
hotwords separated by ``/``.
Return:
An ``OnlineStream`` object.
)doc";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The docstring kCreateStreamDoc describes a hotwords argument, but it is being applied to the no-argument overload of create_stream at line 222. This results in misleading documentation for the Python API. It is better to split this into two separate docstrings to accurately reflect the arguments for each overload, following the pattern used in offline-recognizer.cc.

static constexpr const char *kCreateStreamDoc = R"doc(
Create a new OnlineStream for decoding.

Return:
  An OnlineStream object.
)doc";

static constexpr const char *kCreateStreamHotwordsDoc = R"doc(
Create a new OnlineStream for decoding.

Args:
  hotwords:
    Optional hotwords for this stream. If provided, it is a string of
    hotwords separated by /.
Return:
  An OnlineStream object.
)doc";

},
py::arg("hotwords"), py::call_guard<py::gil_scoped_release>())
.def("is_ready", &PyClass::IsReady,
py::arg("hotwords"), kCreateStreamDoc,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Use the new docstring that correctly describes the hotwords argument for this overload.

Suggested change
py::arg("hotwords"), kCreateStreamDoc,
py::arg("hotwords"), kCreateStreamHotwordsDoc,

Comment on lines +58 to +67
static constexpr const char *kCreateStreamDoc = R"doc(
Create a new streaming recognition instance.

Args:
keywords:
A string of keywords to spot, separated by ``/``.

Returns:
An ``OnlineStream`` object.
)doc";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The docstring kCreateStreamDoc describes a keywords argument but is applied to the no-argument overload at line 115. Conversely, the overload that actually takes keywords at line 121 is missing the docstring. These should be split to ensure accurate documentation for both overloads.

static constexpr const char *kCreateStreamDoc = R"doc(
Create a new streaming recognition instance.

Returns:
  An OnlineStream object.
)doc";

static constexpr const char *kCreateStreamKeywordsDoc = R"doc(
Create a new streaming recognition instance.

Args:
  keywords:
    A string of keywords to spot, separated by /.

Returns:
  An OnlineStream object.
)doc";

[](PyClass &self, const std::string &keywords) {
return self.CreateStream(keywords);
},
py::arg("keywords"), py::call_guard<py::gil_scoped_release>())

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Add the docstring that correctly describes the keywords argument for this overload.

Suggested change
py::arg("keywords"), py::call_guard<py::gil_scoped_release>())
py::arg("keywords"), kCreateStreamKeywordsDoc, py::call_guard<py::gil_scoped_release>())

@csukuangfj
csukuangfj merged commit bdeb8b7 into k2-fsa:master May 27, 2026
1 check passed
@csukuangfj
csukuangfj deleted the python-api-doc branch May 27, 2026 01:52
jimmy1984xu pushed a commit to jimmy1984xu/sherpa-onnx that referenced this pull request Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant