Skip to content

Python: fix(python): respect type alias and default type in KernelParameterMetadata schema inference - #14705

Open
Hardik K (hardikkaurani) wants to merge 3 commits into
microsoft:mainfrom
hardikkaurani:fix/kernel-parameter-metadata-type-alias
Open

Hardik K (hardikkaurani) wants to merge 3 commits into
microsoft:mainfrom
hardikkaurani:fix/kernel-parameter-metadata-type-alias

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Description

When instantiating KernelParameterMetadata, the parameter type can be provided either via type_="..." or via its declared alias type="..." (or defaulted to "str").

However, form_schema runs as a mode="before" model validator prior to alias resolution and default population in Pydantic. Inside form_schema:

type_ = data.get("type_", None)

This caused two issues:

  1. When callers initialize metadata with the alias type="..." (e.g., KernelParameterMetadata(name="query", type="str")), data contains "type", so data.get("type_") evaluates to None. As a result, infer_schema returns None and metadata.schema_data remains None.
  2. When callers initialize metadata without specifying a type (relying on the default type_: str | None = Field(default="str", alias="type")), type_ is also absent from data during mode="before", leaving metadata.schema_data as None instead of inferring {"type": "string"}.

When tool-calling or function-choice schemas are constructed across connectors (OpenAI, Anthropic, Bedrock, etc.), missing schema_data serializes parameter properties as null instead of their proper JSON schema.

Fix

  • In KernelParameterMetadata.form_schema, check data.get("type_") or data.get("type").
  • If neither was provided and no type_object was specified, fall back to "str" matching the field default.
  • Added regression tests verifying schema inference when using the type alias and when using the default type.

Verification

  • Added regression tests in python/tests/unit/functions/test_kernel_parameter_metadata.py.
  • All unit tests in python/tests/unit/functions/ pass (208 passed).
  • Ruff check and formatting checks pass cleanly.

Copilot AI balanced review requested due to automatic review settings November 1, 2026 17:22
@hardikkaurani
Hardik K (hardikkaurani) requested a review from a team as a code owner November 1, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

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!

@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Nov 1, 2026
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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@hardikkaurani Hardik K (hardikkaurani) Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi Eduard van Valkenburg (@eavanvalkenburg), updated per feedback. Switched from truthiness evaluation to explicit key-presence checks ("type_" in data, then "type" in data), ensuring explicit empty strings (type_="" or type="") are preserved and infer {"type": "object"} schema data. Verified locally against test_multiple_param_in_prompt and added dedicated regression tests in test_kernel_parameter_metadata.py.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 both
  • type="int" (alias): None, then {"type": "integer"}
  • no type at all: None, then {"type": "string"}
  • type="": None, then {"type": "object"}
  • type_object=int or list[str] without a type: same on both
  • type_=None passed explicitly: None on both
  • schema_data passed 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.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Thanks for the review PRABHU KIRAN VANDRANKI (@VANDRANKI) !

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Eduard van Valkenburg (@eavanvalkenburg) please take a look.

This branch was previously deployed

1 inactive deployment
github-app-auth — da47f274 Deployed Nov 2, 2026 by hardikkaurani via add_label #29539
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants