Skip to content

fix(genai): guard against None parameters in dict tool conversion - #1892

Open
Romil Shah (romil2807) wants to merge 1 commit into
langchain-ai:mainfrom
romil2807:fix/genai-titleless-dict-schema-none-guard
Open

fix(genai): guard against None parameters in dict tool conversion#1892
Romil Shah (romil2807) wants to merge 1 commit into
langchain-ai:mainfrom
romil2807:fix/genai-titleless-dict-schema-none-guard

Conversation

@romil2807

Copy link
Copy Markdown

Description

with_structured_output(schema, method="function_calling") raises AttributeError: 'NoneType' object has no attribute 'get' when schema is a dict without a top-level "title" key.

Root cause: Two problems compound:

  1. bind_tools's fallback (triggered when convert_to_openai_tool rejects a titleless dict) produces a FunctionDeclaration(name="MISSING_NAME", parameters=None) via _format_to_genai_function_declaration's else branch.
  2. When _prepare_request later re-parses the stored {"function_declarations": [...]} tools, _format_to_genai_function_declaration hits tool["parameters"].get("properties") — but "parameters" is present with value None, so .get() crashes.

Fix: Add a None check before calling .get("properties") on tool["parameters"].

Scope note

Problem 1 (the bind_tools fallback silently producing a MISSING_NAME/parameters=None declaration 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

  • Added test_format_to_genai_function_declaration_none_parameters (direct unit test) and test_convert_to_genai_function_declarations_none_parameters (end-to-end, mirrors the actual _prepare_request code path) to libs/genai/tests/unit_tests/test_function_utils.py.
  • Verified locally: make test (336 passed), make lint (ruff + mypy clean).

Fixes #1807

_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant