Skip to content

Generate pyd type info#2247

Open
themason2011 wants to merge 4 commits into
mainfrom
generate-pyd-type-info
Open

Generate pyd type info#2247
themason2011 wants to merge 4 commits into
mainfrom
generate-pyd-type-info

Conversation

@themason2011

Copy link
Copy Markdown

Adds pybind11-stubgen based type stub generation for the onnxruntime_genai Python package.

Changes:

  • Add ElementType enum binding in python.cpp
  • Add fix_pyi.py to strip the Oga prefix from generated stubs
  • Add py.typed PEP 561 marker
  • Add init.pyi.in re-export stub
  • Wire up CMake POST_BUILD steps to generate and fix type stubs

Replicates the changes from PR #1742.

Add pybind11-stubgen based type stub generation for the onnxruntime_genai Python package, including ElementType enum binding, fix_pyi.py post-processing, py.typed marker, and CMake POST_BUILD wiring.
Copilot AI review requested due to automatic review settings June 29, 2026 20:48
@themason2011 themason2011 requested a review from a team as a code owner June 29, 2026 20:48
@themason2011

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Microsoft"

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds Python typing support to the onnxruntime_genai wheel by generating .pyi stubs from the pybind11 extension at build time (plus a small stub post-processor), and exposes an ElementType enum needed by the public Python surface.

Changes:

  • Bind OgaElementType into Python as ElementType in the pybind11 module.
  • Add build-time stub generation + stub-fix step (plus py.typed and an __init__.pyi re-export) so the wheel ships type information.
  • Add fix_pyi.py to rewrite generated stubs to use Python-facing names instead of C++/C API names.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/python/python.cpp Exposes ElementType enum via pybind11 so it appears in the Python API (and stubs).
src/python/py/py.typed Adds PEP 561 marker so type checkers treat the package as typed.
src/python/fix_pyi.py Post-processes generated stubs to strip the Oga prefix from type names.
src/python/CMakeLists.txt Adds POST_BUILD stub generation and wires generated/fixed stubs into the wheel layout.
src/python/init.pyi.in Provides a re-exporting package-level stub that mirrors runtime imports.

Comment thread src/python/fix_pyi.py Outdated
Comment on lines +7 to +17
from argparse import ArgumentParser
from pathlib import Path

parser = ArgumentParser()
parser.add_argument("--input", "-i", required=True, help="Input .pyi file")
args = parser.parse_args()

pyi_file = Path(args.input)
content = pyi_file.read_text()
fixed_content = content.replace("Oga", "")
pyi_file.write_text(fixed_content)
Comment thread src/python/CMakeLists.txt Outdated
Comment on lines +65 to +67
# Be compatible with both system-wide but unprivileged env and virtual env where --user is not available
COMMAND ${PYTHON_EXECUTABLE} -m pip install --user pybind11-stubgen numpy || ${PYTHON_EXECUTABLE} -m pip install pybind11-stubgen numpy
COMMAND ${PYTHON_EXECUTABLE} -m pybind11_stubgen $<TARGET_FILE_BASE_NAME:python> --output-dir ${WHEEL_TARGET_NAME}
Comment thread src/python/CMakeLists.txt Outdated
Comment on lines +63 to +69
COMMAND ${CMAKE_COMMAND} -E copy_directory ${WHEEL_TARGET_NAME} ${GENERATE_TYPE_DIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${ORT_LIB_DIR} ${GENERATE_TYPE_DIR}
# Be compatible with both system-wide but unprivileged env and virtual env where --user is not available
COMMAND ${PYTHON_EXECUTABLE} -m pip install --user pybind11-stubgen numpy || ${PYTHON_EXECUTABLE} -m pip install pybind11-stubgen numpy
COMMAND ${PYTHON_EXECUTABLE} -m pybind11_stubgen $<TARGET_FILE_BASE_NAME:python> --output-dir ${WHEEL_TARGET_NAME}
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/fix_pyi.py -i ${WHEEL_TARGET_NAME}/$<TARGET_FILE_BASE_NAME:python>.pyi
WORKING_DIRECTORY ${GENERATE_TYPE_DIR}
Comment thread src/python/CMakeLists.txt Outdated
set(WHEEL_TARGET_NAME "${WHEEL_FILES_DIR}/${PACKAGE_DIR_NAME}")
set(GENERATE_TYPE_DIR_NAME "onnxruntime_genai-generate-type")
set(GENERATE_TYPE_DIR "${WHEEL_FILES_DIR}/${GENERATE_TYPE_DIR_NAME}")
file(MAKE_DIRECTORY ${GENERATE_TYPE_DIR})
Copilot AI added 2 commits June 29, 2026 22:31
…ists.txt

- fix_pyi.py: use regex with word boundary to strip Oga prefix only at
  identifier boundaries, and read/write files with explicit UTF-8 encoding
- CMakeLists.txt: quote path variables to handle spaces in build directory;
  install stubgen tools to an isolated target directory to avoid polluting
  global/user site-packages"
@kunal-vaishnavi

Copy link
Copy Markdown
Contributor

Have the previously detected issues with pyd type info been resolved in the packaging pipelines?

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.

4 participants