Repository navigation
Python: fix(python): respect type alias and default type in KernelParameterMetadata schema inference - #14705
Conversation
…lias and default schema
…tadata schema inference
|
Hi Eduard van Valkenburg (@eavanvalkenburg), could you please take a look at this PR when you have a moment? It fixes schema_data inference in KernelParameterMetadata when using the 'type' alias or when relying on the default 'str' type. Tests and ruff checks pass cleanly. Thank you! |
| if isinstance(data, dict) and data.get("schema_data") is None: | ||
| type_object = data.get("type_object", None) | ||
| type_ = data.get("type_", None) | ||
| type_ = data.get("type_") or data.get("type") |
There was a problem hiding this comment.
[P2] Preserve explicit empty-string parameter types
Using or here changes an explicit type_="" into None. KernelFunctionFromPrompt("test", prompt="{{$param}}{{$param}}") creates exactly this metadata, so its parameter now has schema_data=None instead of {"type": "object"}. The existing tests/unit/prompt_template/test_prompt_templates.py::test_multiple_param_in_prompt fails both locally and in the newly started unit-test/coverage workflows. Please choose between type_ and its alias by key presence (preserving the supplied empty string), rather than truthiness, and include this existing prompt test in the regression coverage.
There was a problem hiding this comment.
Addressed. Switched to key-presence checks ("type_" in data, then "type" in data) so explicitly provided empty strings (type_="" or type="") are preserved instead of being coalesced to None by truthiness. Verified with test_multiple_param_in_prompt and added regression tests for both explicit empty-string paths.
|
Hi Eduard van Valkenburg (@eavanvalkenburg), updated per feedback. Switched from truthiness evaluation to explicit key-presence checks ( |
PRABHU KIRAN VANDRANKI (VANDRANKI)
left a comment
There was a problem hiding this comment.
I ran this on current main (df1cb33) and on main with this diff applied (python venv). The diff applied cleanly.
Main reproduces the bug, and the effect reaches the tool schema. I built three parameters the way the PR describes and rendered them with kernel_function_metadata_to_function_call_format:
K(name="query", description="what", type="str", is_required=True),K(name="limit", type="int", default_value=5, is_required=False),K(name="flag")- main:
"properties": {"query": null, "limit": null, "flag": null} - PR:
"query": {"type": "string", "description": "what"},"limit": {"type": "integer", "description": "(default value: 5)"},"flag": {"type": "string"}
Constructor cases (schema_data, main then PR):
type_="int":{"type": "integer"}on bothtype="int"(alias): None, then{"type": "integer"}- no type at all: None, then
{"type": "string"} type="": None, then{"type": "object"}type_object=intorlist[str]without a type: same on bothtype_=Nonepassed explicitly: None on bothschema_datapassed explicitly: kept on both
Things that use the alias today are unchanged. DEFAULT_PARAMETER_METADATA and DEFAULT_RETURN_PARAMETER_METADATA in data/_shared.py use type="str" and type_object=; their schema_data is identical on main and the PR. Prompt functions and method functions pass type_, and their schemas are identical too. The one in-repo constant that changes is PROMPT_RETURN_PARAM (built with type="FunctionResult"): schema_data goes from None to {"type": "object", "description": "The completion result"}. I did not find a connector that reads a return parameter's schema, but I only grepped.
Tests. test_kernel_parameter_metadata.py: 7 passed on main, 11 on the PR. If I copy the PR's test file onto main, 4 fail (init, type_alias, default_type, explicit_empty_type_alias). I also ran 14 related unit test directories (functions, prompt_template, mcp, openapi_plugin, bedrock_agent, orchestration, data, kernel, schema, utils, and the open_ai, anthropic, bedrock, google connector tests). 56 tests fail or error on main and exactly the same 56 on the PR. I did not look into why they fail here, but nothing new fails.
A nit, not a blocker. If a caller passes both keys, type="int", type_="str", pydantic sets type_ to int through the alias, and the validator picks data["type_"] first, so schema_data says string. That mismatch is the same on main (I ran both). The new elif order keeps it, so it could be flipped to match the alias, but both keys together look like a caller mistake.
Approving.
|
Thanks for the review PRABHU KIRAN VANDRANKI (@VANDRANKI) ! |
|
Eduard van Valkenburg (@eavanvalkenburg) please take a look. |
Description
When instantiating
KernelParameterMetadata, the parameter type can be provided either viatype_="..."or via its declared aliastype="..."(or defaulted to"str").However,
form_schemaruns as amode="before"model validator prior to alias resolution and default population in Pydantic. Insideform_schema:This caused two issues:
type="..."(e.g.,KernelParameterMetadata(name="query", type="str")),datacontains"type", sodata.get("type_")evaluates toNone. As a result,infer_schemareturnsNoneandmetadata.schema_dataremainsNone.type_: str | None = Field(default="str", alias="type")),type_is also absent fromdataduringmode="before", leavingmetadata.schema_dataasNoneinstead of inferring{"type": "string"}.When tool-calling or function-choice schemas are constructed across connectors (OpenAI, Anthropic, Bedrock, etc.), missing
schema_dataserializes parameter properties asnullinstead of their proper JSON schema.Fix
KernelParameterMetadata.form_schema, checkdata.get("type_") or data.get("type").type_objectwas specified, fall back to"str"matching the field default.typealias and when using the default type.Verification
python/tests/unit/functions/test_kernel_parameter_metadata.py.python/tests/unit/functions/pass (208 passed).