Skip to content

fix(typesafe): reject empty questions in TypeSafeClassifier - #41241

Open
Champions Zhang (ChampionsZhang) wants to merge 8 commits into
langchain-ai:masterfrom
ChampionsZhang:ChampionsZhang/typesafe/validate-classifier-requests
Open

Champions Zhang (ChampionsZhang) wants to merge 8 commits into
langchain-ai:masterfrom
ChampionsZhang:ChampionsZhang/typesafe/validate-classifier-requests

Conversation

@ChampionsZhang

Copy link
Copy Markdown
Contributor

Fixes #40706


Replaces #40707, which was automatically closed when my source fork was deleted. This PR restores the exact same branch and commits; the previous discussion and community review are available in #40707.

Direct TypeSafeClassifier callers can build an empty question mapping dynamically, but invocation-scoped ClassifierRequest no longer enforces the former min_length=1, so invalid requests reach the API. This restores local validation in the shared request-building path and covers both synchronous and asynchronous execution without calling either HTTP client.

Release note

TypeSafeClassifier now raises a ValueError before sending requests whose questions mapping is empty.

This contribution was developed with assistance from an AI coding agent and reviewed by me.

@github-actions github-actions Bot added fix For PRs that implement a fix integration PR made that is related to a provider partner package integration size: XS < 50 LOC typesafe labels Oct 14, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for the PR! I inspected the changes and the added unit test.

The validation logic cleanly guards _payload by checking if not request["questions"]: and raising a ValueError, which correctly prevents empty requests from hitting the network layer. The added unit test covers both synchronous and asynchronous paths (invoke and ainvoke) using mock clients to ensure no HTTP requests are dispatched on invalid input.

Everything looks solid and well-tested. Good catch restoring this validation path!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Champions Zhang (@ChampionsZhang) d1cb7e3 restores the empty-questions rejection. From my perspective this can be merged. #40706 already traces the lost Field(min_length=1) to #40659. I checked two things only: the new test catches it, and nothing reaches the HTTP client.

$ python -m pytest tests/unit_tests -q   # libs/partners/typesafe at d1cb7e3, CPython 3.14.7
93 passed in 1.84s

$ python -m pytest tests/unit_tests -q   # classifier.py from 8ace87e, everything else from d1cb7e3
2 failed, 91 passed, 2 warnings in 1.15s
FAILED tests/unit_tests/test_classifier.py::test_empty_questions_are_rejected_before_request[False]
FAILED tests/unit_tests/test_classifier.py::test_empty_questions_are_rejected_before_request[True]

$ python probe41241.py   # invoke({"state": "hi", "questions": {}}) with a callback handler, at 16ccdd3 (d1cb7e3 only merges master)
raised: ValueError TypeSafe classifier requests must contain at least one question.
callbacks: ['chain_start', 'chain_error:ValueError'] | post called: False

Neither middleware hits the new error: AutoModeMiddleware.tools and ModelRouterMiddleware.choices keep Field(min_length=1).

non-blocking: on the open question in #40706, I'd keep the check in the shared request path. A bad call then shows up as a failed run.

Is anything still missing before this can merge?

@ChampionsZhang

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and verification! Really appreciate it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external fix For PRs that implement a fix integration PR made that is related to a provider partner package integration size: XS < 50 LOC typesafe

Projects

None yet

Development

Successfully merging this pull request may close these issues.

langchain-typesafe: TypeSafeClassifier sends requests when questions is empty

3 participants

⌂ Proxy