fix(sqlserver): use not_in for $nin metadata filter operator - #853
Merged
Folashade Daniel (beccadaniel) merged 1 commit intoJul 22, 2026
Conversation
Re-lands the fix from langchain-ai#812, which was reverted in langchain-ai#852 because its test broke CI. `_handle_field_filter` called `queried_field.nin_(...)` for the `$nin` operator, but SQLAlchemy has no `nin_` method, so any `$nin` filter raised AttributeError at query time. Use `not_in`, the supported API. The test helper builds the store via `__new__`, so it must set every attribute `_get_embedding_store` reads. It now also sets `_use_binary_collation`, which that method started requiring after the binary-collation option landed -- the omission is what failed CI and caused the revert.
This was referenced Jul 22, 2026
Copilot started reviewing on behalf of
Folashade Daniel (beccadaniel)
July 22, 2026 18:29
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes SQL Server metadata filtering for the $nin operator by using SQLAlchemy’s supported not_in operator, and reintroduces unit coverage that validates the compiled SQL without requiring a live database.
Changes:
- Replace the invalid
nin_call withnot_infor$ninmetadata filters in_handle_field_filter. - Add unit tests that compile filter expressions to MSSQL SQL to validate
$nin,$in, and$like. - Update the test store factory to set
_use_binary_collationso_get_embedding_storeworks when constructing via__new__.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| libs/sqlserver/langchain_sqlserver/vectorstores.py | Fix $nin implementation to use not_in instead of a nonexistent nin_ method. |
| libs/sqlserver/tests/unit_tests/test_filters.py | Add compile-only unit tests for $nin/$in/$like and ensure _use_binary_collation is set in the test helper. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Folashade Daniel (beccadaniel)
approved these changes
Jul 22, 2026
Folashade Daniel (beccadaniel)
merged commit Jul 22, 2026
42f166f
into
langchain-ai:main
8 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Re-opens #812, which was merged and then reverted in #852 because the test it added broke CI.
The original bug (still present on
mainafter the revert):_handle_field_filterhandles the$ninoperator withSQLAlchemy has no
nin_method, so any$ninmetadata filter raisesAttributeErrorat query time. The supported API isnot_in.What caused the revert, and the fix
tests/unit_tests/test_filters.pybuilds the store with__new__(to avoid needing a live DB), so it must set every attribute_get_embedding_storereads. Between when #812 was opened and when it merged, theuse_binary_collationwork (#801) landed and made_get_embedding_storereadself._use_binary_collation. The test helper did not set it, so once #812 merged,mainfailed with:This PR is the original one-line source fix plus the test helper now setting
_use_binary_collation = False. Rebased on currentmain, so it accounts for the collation change.Testing
tests/unit_tests/test_filters.pycovers$nin,$inand$likeby compiling the filter expression to SQL, so no live database is required. Verified against currentmain: full sqlserver unit suite passes (17 passed, 1 skipped) andmake lint(ruff + ruff format + mypy) is clean.Sorry for the CI breakage on the first attempt -- I have double-checked this one against the post-collation
main.