fix(genai): guard against None parameters in dict tool conversion - #1892
Open
Romil Shah (romil2807) wants to merge 1 commit into
Open
fix(genai): guard against None parameters in dict tool conversion#1892Romil Shah (romil2807) wants to merge 1 commit into
Romil Shah (romil2807) wants to merge 1 commit into
Conversation
_format_to_genai_function_declaration crashes with AttributeError when a
dict tool has a "parameters" key set to None (e.g. produced by bind_tools's
fallback path for a titleless dict schema, then re-parsed a second time by
_prepare_request). Add a None check before calling .get("properties") on it.
Fixes langchain-ai#1807
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
with_structured_output(schema, method="function_calling")raisesAttributeError: 'NoneType' object has no attribute 'get'whenschemais a dict without a top-level"title"key.Root cause: Two problems compound:
bind_tools's fallback (triggered whenconvert_to_openai_toolrejects a titleless dict) produces aFunctionDeclaration(name="MISSING_NAME", parameters=None)via_format_to_genai_function_declaration'selsebranch._prepare_requestlater re-parses the stored{"function_declarations": [...]}tools,_format_to_genai_function_declarationhitstool["parameters"].get("properties")— but"parameters"is present with valueNone, so.get()crashes.Fix: Add a
Nonecheck before calling.get("properties")ontool["parameters"].Scope note
Problem 1 (the
bind_toolsfallback silently producing aMISSING_NAME/parameters=Nonedeclaration for titleless dict schemas, instead of raising a clearer error or handling the schema properly) is a separate, deeper issue. This PR only fixes the crash (Problem 2) — happy to follow up on Problem 1 separately if maintainers want it addressed.Testing
test_format_to_genai_function_declaration_none_parameters(direct unit test) andtest_convert_to_genai_function_declarations_none_parameters(end-to-end, mirrors the actual_prepare_requestcode path) tolibs/genai/tests/unit_tests/test_function_utils.py.make test(336 passed),make lint(ruff + mypy clean).Fixes #1807