Skip to content

Commit

Permalink
feat: generate types.py from genkit-schema.json using datamodel-codegen
Browse files Browse the repository at this point in the history
#1807

    ISSUE: #1807

    CHANGELOG:
    - [ ] Add a bin/generate_schema_types script that generates the Pydantic
      types.py module.
    - [ ] Update the pre-commit hooks to ensure this file gets regenerated
      routinely.
    - [ ] Remove timestamp to ensure we do not treat a file with identical
      content differently preventing the hassles of updating this file per
    commit.
  • Loading branch information
yesudeep committed Feb 3, 2025
1 parent df5ce31 commit 5cbb967
Show file tree
Hide file tree
Showing 5 changed files with 506 additions and 249 deletions.
33 changes: 33 additions & 0 deletions py/bin/generate_schema_types
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
#
# Copyright 2025 Google LLC
# SPDX-License-Identifier: Apache-2.0

set -euo pipefail

TOP_DIR=$(git rev-parse --show-toplevel)

uv run --directory "$TOP_DIR/py" \
datamodel-codegen \
--collapse-root-models \
--disable-timestamp \
--enable-version-header \
--input "$TOP_DIR/genkit-tools/genkit-schema.json" \
--input-file-type jsonschema \
--output-model-type pydantic_v2.BaseModel \
--output "$TOP_DIR/py/packages/genkit/src/genkit/core/types.py" \
--target-python-version 3.12 \
--use-schema-description \
--use-standard-collections \
--use-union-operator

addlicense \
-c "Google LLC" \
-s=only \
"$TOP_DIR/py/packages/genkit/src/genkit/core/types.py"

uv run --directory "$TOP_DIR/py" \
ruff check --fix "${TOP_DIR}/py/packages/genkit/src/genkit/core/types.py"

uv run --directory "$TOP_DIR/py" \
ruff format "${TOP_DIR}/py/packages/genkit/src/genkit/core/types.py"
6 changes: 6 additions & 0 deletions py/captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
{
"run": "pnpm i --frozen-lockfile"
},
{
"run": "py/bin/generate_schema_types"
},
{
"run": "py/bin/fmt"
},
Expand Down Expand Up @@ -81,6 +84,9 @@
{
"run": "pnpm i --frozen-lockfile"
},
{
"run": "py/bin/generate_schema_types"
},
{
"run": "py/bin/fmt"
},
Expand Down
Loading

0 comments on commit 5cbb967

Please sign in to comment.