Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(py): genkit.core.schemas -> genkit.core.schema_types since we also have a schema module #2011

Merged
merged 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions py/bin/generate_schema_types
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
set -euo pipefail

TOP_DIR=$(git rev-parse --show-toplevel)
SCHEMA_FILE="${TOP_DIR}/py/packages/genkit/src/genkit/core/schemas.py"
SCHEMA_FILE="${TOP_DIR}/py/packages/genkit/src/genkit/core/schema_types.py"

# Generate types using configuration from pyproject.toml
uv run --directory "${TOP_DIR}/py" datamodel-codegen
Expand All @@ -15,7 +15,7 @@ uv run --directory "${TOP_DIR}/py" datamodel-codegen
#sed -i '' '/^class Model(RootModel\[Any\]):$/,/^ root: Any$/d' "${SCHEMA_FILE}"

# Sanitize the generated schema.
python3 "${TOP_DIR}/py/bin/sanitize_schemas.py" "${SCHEMA_FILE}"
python3 "${TOP_DIR}/py/bin/sanitize_schema_types.py" "${SCHEMA_FILE}"

# Checks and formatting.
uv run --directory "${TOP_DIR}/py" \
Expand Down
14 changes: 8 additions & 6 deletions py/bin/sanitize_schemas.py → py/bin/sanitize_schema_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# SPDX-License-Identifier: Apache-2.0


"""Standalone convenience script used to massage the schemas.py.
"""Standalone convenience script used to massage the schema_types.py.

The `py/packages/genkit/src/genkit/core/schemas.py` file is generated by
The `py/packages/genkit/src/genkit/core/schema_types.py` file is generated by
datamodel-codegen. However, since the tool doesn't currently provide options to
generate exactly the kind of code we need, we use this convenience script to
parse the Python source code, walk the AST, modify it to include the bits we
Expand All @@ -21,8 +21,8 @@
Pythonic API but serialize to camelCase in order to be compatible with
runtimes.
- We add a license header
- We add a header indicating that this file has been generated by a code generator
pass.
- We add a header indicating that this file has been generated by a code
generator pass.
- We add the ability to use forward references.
"""

Expand Down Expand Up @@ -83,7 +83,8 @@ def has_model_config(self, node: ast.ClassDef) -> bool:
return False

def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef: # noqa: N802
"""Visit class definitions and handle model_config based on class type."""
"""Visit class definitions and handle model_config based on class
type."""
if self.is_rootmodel_class(node):
# Filter out model_config assignments for RootModel classes
new_body = []
Expand Down Expand Up @@ -111,7 +112,8 @@ def visit_ClassDef(self, node: ast.ClassDef) -> ast.ClassDef: # noqa: N802
node.body.insert(0, self.create_model_config())
self.modified = True
else:
# Update existing model_config to include populate_by_name=True
# Update existing model_config to include
# populate_by_name=True
new_body = []
for item in node.body:
if isinstance(item, ast.Assign):
Expand Down
2 changes: 1 addition & 1 deletion py/packages/genkit/src/genkit/ai/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

from collections.abc import Callable

from genkit.core.schemas import GenerateRequest, GenerateResponse
from genkit.core.schema_types import GenerateRequest, GenerateResponse

ModelFn = Callable[[GenerateRequest], GenerateResponse]
2 changes: 1 addition & 1 deletion py/packages/genkit/src/genkit/ai/prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
from collections.abc import Callable
from typing import Any

from genkit.core.schemas import GenerateRequest
from genkit.core.schema_types import GenerateRequest

PromptFn = Callable[[Any | None], GenerateRequest]
2 changes: 1 addition & 1 deletion py/packages/genkit/src/genkit/core/plugin_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import abc
import typing

from genkit.core.schemas import GenerateRequest, GenerateResponse
from genkit.core.schema_types import GenerateRequest, GenerateResponse

if typing.TYPE_CHECKING:
from genkit.veneer import Genkit
Expand Down
2 changes: 1 addition & 1 deletion py/packages/genkit/src/genkit/veneer/veneer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from genkit.core.plugin_abc import Plugin
from genkit.core.reflection import make_reflection_server
from genkit.core.registry import Registry
from genkit.core.schemas import GenerateRequest, GenerateResponse, Message
from genkit.core.schema_types import GenerateRequest, GenerateResponse, Message
from genkit.veneer import server

DEFAULT_REFLECTION_SERVER_SPEC = server.ServerSpec(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import vertexai
from genkit.core.plugin_abc import Plugin
from genkit.core.schemas import (
from genkit.core.schema_types import (
GenerateRequest,
GenerateResponse,
Message,
Expand Down
10 changes: 1 addition & 9 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,6 @@ target-version = "py312"

[tool.ruff.lint]
fixable = ["ALL"]
per-file-ignores = { "schemas.py" = [
"N815",
"E501",
], "sanitize_schemas.py" = [
"N802",
"N815",
"E501",
] }
select = [
"E", # pycodestyle (errors)
"W", # pycodestyle (warnings)
Expand Down Expand Up @@ -166,7 +158,7 @@ enable-version-header = true
field-constraints = true
input = "../genkit-tools/genkit-schema.json"
input-file-type = "jsonschema"
output = "packages/genkit/src/genkit/core/schemas.py"
output = "packages/genkit/src/genkit/core/schema_types.py"
output-model-type = "pydantic_v2.BaseModel"
snake-case-field = true
strict-nullable = true
Expand Down
2 changes: 1 addition & 1 deletion py/samples/hello/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from typing import Any

from genkit.core.schemas import GenerateRequest, Message, Role, TextPart
from genkit.core.schema_types import GenerateRequest, Message, Role, TextPart
from genkit.plugins.vertex_ai import VertexAI
from genkit.veneer.veneer import Genkit
from pydantic import BaseModel, Field
Expand Down
Loading