fix(py/typing): respect additionalProperties from JSON schema#4451
fix(py/typing): respect additionalProperties from JSON schema#4451huangjeff5 merged 1 commit intomainfrom
Conversation
Summary of ChangesHello @yesudeep, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical data handling inconsistency between Python and JavaScript schema implementations. By enhancing the Python schema generation script to interpret Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request updates the Python schema sanitizer to respect the additionalProperties: true setting from the JSON schema, which brings it to parity with the JavaScript implementation. The changes correctly load the schema, identify models that allow extra properties, and adjust the Pydantic model generation accordingly. The implementation is solid. I've added one suggestion to improve the robustness of how the script locates the schema file, making it less dependent on a fixed directory structure.
The Python schema sanitizer was always setting extra='forbid' for all Pydantic models, ignoring the additionalProperties setting from the canonical JSON schema. This fix: - Reads genkit-schema.json to identify models with additionalProperties: true - Handles both top-level $defs and inline nested objects (e.g., Score.details) - Uses extra='allow' for those models (equivalent to .passthrough() in Zod) - Uses extra='forbid' for all other models (default behavior) Models now correctly using extra='allow': - RankedDocumentMetadata (reranker metadata preservation) - Details (Score.details for evaluation reasoning) - Error (Operation.error for error details) This matches the JavaScript .passthrough() behavior and ensures JS/Python parity for type validation.
6418cc9 to
300a118
Compare
Summary
Fixes the Python schema generator to respect
additionalProperties: truefrom the canonical JSON schema, matching JavaScript behavior for models using.passthrough()in Zod.Problem
The Python schema sanitizer (
py/bin/sanitize_schema_typing.py) was always settingextra='forbid'for all Pydantic models, regardless of theadditionalPropertiessetting in the JSON schema. This caused a parity issue with the JavaScript implementation where models use.passthrough()to allow extra properties.Impact:
reasoningwere rejectedmessagewere rejectedSolution
Updated the schema sanitizer to:
genkit-tools/genkit-schema.jsonat generation timeadditionalProperties: true:$defs(e.g.,RankedDocumentMetadata)Score.details→Detailsclass,Operation.error→Errorclass)extra='allow'for those models (equivalent to.passthrough()in Zod)extra='forbid'for all other models (default behavior)Changes
py/bin/sanitize_schema_typing.pyload_models_allowing_extra()function to read JSON schema and identify passthrough models (both top-level and inline). UpdatedClassTransformerto useextra='allow'for those models.py/packages/genkit/src/genkit/core/typing.pyextra='allow'instead ofextra='forbid'Affected Models
RankedDocumentMetadata$defsDetailsScore.detailsErrorOperation.errorBefore/After
Before:
After:
Testing
extra='allow'Related