Skip to content

PydanticToolsParser raises KeyError when partial=True and an unmatched tool is present #34910

@Alioth99

Description

@Alioth99

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-perplexity
  • langchain-prompty
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Reproduction Steps / Example Code (Python)

from langchain_core.output_parsers.openai_tools import PydanticToolsParser
from pydantic import BaseModel
from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatGeneration

class Model1(BaseModel):
    name: str
    age: int

class Model2(BaseModel):
    value: float

parser = PydanticToolsParser(tools=[Model1, Model2])
result = ChatGeneration(
    message=AIMessage(
        content="",
        additional_kwargs={
            "tool_calls": [
                {"type": "function", "function": {"name": "Model1", "arguments": '{"name": "Alice", "age": 30}'}},
                {"type": "function", "function": {"name": "Model3", "arguments": '{"value": 3.14}'}}
            ]
        }
    )
)
output = parser.parse_result([result], partial=True)

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/home/workplace/lc4.py", line 25, in <module>
    output = parser.parse_result([result], partial=True)
  File "/lib/python3.10/site-packages/langchain_core/output_parsers/openai_tools.py", line 306, in parse_result
    pydantic_objects.append(name_dict[res["type"]](**res["args"]))
KeyError: 'Model3'

Description

When using PydanticToolsParser.parse_result with partial=True, if the LLM output contains a tool call referencing a tool that is not registered in the parser, a KeyError is raised.

From the API documentation, partial=True is expected to allow safely skipping partially parseable outputs. However, the current implementation does not account for unmatched tool names, so even with partial=True, the method crashes instead of skipping the unknown tool.

This seems to be purely an internal logic oversight: the code checks partial for argument type or validation errors, but it does not handle the case where res["type"] is not in name_dict, leading to an unhandled KeyError.

System Info

Package Information

langchain_core: 0.3.68
langchain: 0.3.26
langsmith: 0.4.5
langchain_text_splitters: 0.3.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelated to a bug, vulnerability, unexpected error with an existing featurecore`langchain-core` package issues & PRsexternal

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions