Skip to content

Python: Build array and mapping schemas for collections.abc Sequence and Mapping - #14796

Open
Hardik K (hardikkaurani) wants to merge 7 commits into
microsoft:mainfrom
hardikkaurani:fix/schema-builder-sequence-mapping
Open

Hardik K (hardikkaurani) wants to merge 7 commits into
microsoft:mainfrom
hardikkaurani:fix/schema-builder-sequence-mapping

Conversation

@hardikkaurani

Copy link
Copy Markdown
Contributor

Motivation and Context

In Python, function parameters annotated with abstract collections such as collections.abc.Sequence[T], collections.abc.MutableSequence[T], collections.abc.Mapping[str, V], or collections.abc.MutableMapping[str, V] (or their typing counterparts) previously failed to produce typed container schemas in KernelJsonSchemaBuilder.

Instead, Sequence[T] and Mapping[str, V] fell through to cls.get_json_schema(parameter_type) and were emitted as bare {"type": "object", "properties": {}} schemas. This told LLMs that the parameters expect empty objects instead of typed arrays or typed key-value mappings. Additionally, generic aliases over collections.abc classes expose __annotations__, causing KernelJsonSchemaBuilder.build to route them to build_model_schema before inspecting their origin.

Description

  • Route generic types to handle_complex_type before hasattr(parameter_type, "__annotations__") by checking get_origin(parameter_type) is not None or hasattr(parameter_type, "__args__").
  • Extend sequence origin handling in handle_complex_type to recognize Sequence and MutableSequence, emitting standard {"type": "array", "items": ...} schemas.
  • Extend mapping origin handling in handle_complex_type to recognize Mapping and MutableMapping, emitting standard {"type": "object", "additionalProperties": ...} schemas.
  • Added comprehensive unit tests in tests/unit/schema/test_schema_builder.py covering Sequence, MutableSequence, Mapping, MutableMapping, optional wrappers, descriptions, and structured output flags.

Contribution Checklist

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

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.

@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Nov 15, 2026
@hardikkaurani

Hardik K (hardikkaurani) commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi Vincent Biret (@baywet),

Could you please take a look when you have a moment?

This PR enables KernelJsonSchemaBuilder to build typed array and mapping schemas for function parameters annotated with collections.abc.Sequence, MutableSequence, Mapping, and MutableMapping (and their typing counterparts). Previously, generic collections routed to build_model_schema or fell through, emitting bare {"type": "object", "properties": {}} schemas.

All unit tests and lint checks pass cleanly locally. Thank you for your review!

This branch was successfully deployed

1 active deployment
github-app-auth — 2cef06f1 Deployed Nov 15, 2026 by hardikkaurani via add_label #29694
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.

2 participants