Fall back to unstructured output when output model selection fails (#3573) - #3587
Rainmemery wants to merge 1 commit into
Conversation
Picking the output model builds throwaway pydantic models for the return type, which raises PydanticSchemaGenerationError for annotations pydantic cannot model, such as Iterator[str] or AsyncIterator[str]. That call sat outside the try/except that routes expected schema failures to the unstructured fallback, so the error escaped func_metadata and a properly annotated generator tool could not register at all. With structured_output=True the existing flow raises InvalidSignature instead. Fixes modelcontextprotocol#3573
|
This PR has been closed automatically. This repo only keeps pull requests open when they come from a maintainer, or from a contributor a maintainer has assigned to the linked issue, and you aren't currently assigned to #3573. If a maintainer assigns you to #3573, this PR reopens on its own and there's nothing more you need to do here. Assignment is a maintainer call based on capacity; comments that only ask to be assigned don't factor in. What does help is engaging on the issue itself by confirming the repro, explaining why it matters for your use case, or describing the approach you'd take. You're welcome to keep pushing commits here (just avoid force-pushing, since GitHub can't reopen a rewritten branch), but that on its own won't get the PR reviewed or the issue assigned, and realistically most auto-closed PRs stay closed. There's no need to open a new PR either way. CONTRIBUTING.md has the full reasoning, but in short:
Maintainers: reopen, remove |
Fixes #3573
Disclosure: this fix was developed with AI assistance; I have reviewed the diff, run the tests locally, and can explain the change.
Summary
func_metadataraises an uncaughtpydantic.errors.PydanticSchemaGenerationErrorwhen a tool's return annotation isIterator[...]/AsyncIterator[...](either thetypingor thecollections.abcspelling), so a correctly annotated generator tool cannot register at all. The fallback machinery for unserializable return types already exists — the call to_create_output_model()simply sits outside thetrythat routes those failures to it.This wraps that call in the same failure path: on
PydanticSchemaGenerationErrorthe tool degrades to unstructured output (output_schema=None), and withstructured_output=Truethe existing flow raisesInvalidSignature.Generator[...]is untouched — pydantic treats it as a sequence and its structured array schema keeps working.Motivation and Context
-> Iterator[str]is the PEP 484 spelling for generator functions, but registering one currently crashes at decoration time instead of taking the documented unstructured fallback (#3573). Fixing it at the model-selection step reuses the existing fallback semantics in one place rather than special-casing iterator types.How Has This Been Tested?
main(f1b6589) for all four spellings (typing/collections.abc×Iterator/AsyncIterator) with the default andstructured_output=True.tests/server/mcpserver/test_func_metadata.py: 60/60 pass, including two new regression tests (the four spellings fall back to unstructured / raiseInvalidSignature;Generator[str, None, None]keeps its structured array schema).tests/server/mcpserver/tools,tests/interaction/mcpserver/test_tools.py): 85/85 pass.ruff checkandruff format --checkclean.Breaking Changes
None. Registrations that previously crashed now succeed as unstructured tools (or raise
InvalidSignatureunderstructured_output=True, matching the documented contract for unserializable return types).Types of changes
Checklist
help wanted, or I'm a maintainer)