Python: Exclude ClassVar annotations from generated JSON schemas - #14528
Open
Pushpak731 wants to merge 2 commits into
Open
Pushpak731 wants to merge 2 commits into
Pushpak731 wants to merge 2 commits into
Conversation
KernelJsonSchemaBuilder.build_model_schema builds the instance schema
from get_type_hints, which includes ClassVar annotations, so a class
constant became a required object-valued property. Pydantic excludes
ClassVars from model fields and serialized instances, so the generated
schema rejected valid payloads (a serialized {'value': 1} failed
validation because 'marker' was required). The builder now skips
ClassVar-annotated hints (both the parameterized and bare forms,
including inherited ones), for KernelBaseModel subclasses, plain
annotated classes and instances, covering KernelParameterMetadata,
generated tool definitions and structured_output schemas.
Fixes microsoft#14527
Author
|
@microsoft-github-policy-service agree |
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #14527
Description
KernelJsonSchemaBuilder.build_model_schemabuilds the schema fromget_type_hints, which includesClassVarannotations, so a class constant became a required object-valued property in the generated schema. Pydantic excludesClassVars frommodel_fieldsand from serialized instances, so the resulting schema rejected valid serialized payloads ({'value': 1}failed becausemarkerwas required). The same wrong schema reachedKernelParameterMetadata, generated function-call tool definitions andstructured_output=Trueschemas.The fix skips
ClassVar-annotated hints before the required/property loop — covering the parameterized form (ClassVar[str]), the bare form (ClassVar, whoseget_originisNone), and inheritedClassVars, forKernelBaseModelsubclasses, plain annotated classes and model instances alike.Contribution Checklist
tests/unit/schema/(45 passed) andtests/unit/functions/test_kernel_parameter_metadata.py(7 passed) — 10 new regression tests cover pydantic models, bare/inheritedClassVar, plain classes, instances,structured_output, descriptions, optionals and the serialized-payload contractruff checkandruff format --checkare clean on both changed filesmodel_dump)