Skip to content

NotRequired in TypedDict causes TypeError when converting to OpenAI function schema #34085

@roombawulf

Description

@roombawulf

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-cli
  • 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

Example Code (Python)

from typing import NotRequired, TypedDict
from langchain_core.utils.function_calling import convert_to_openai_function

# Simple TypedDict with NotRequired field
class MyTypedDict(TypedDict):
    required_field: str
    optional_field: NotRequired[str]

# This should work but raises TypeError
result = convert_to_openai_function(MyTypedDict)

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "/path/to/langchain_core/utils/function_calling.py", line 396, in convert_to_openai_function
    "dict", _convert_typed_dict_to_openai_function(cast("type", function))
  File "/path/to/langchain_core/utils/function_calling.py", line 213, in _convert_typed_dict_to_openai_function
    _convert_any_typed_dicts_to_pydantic(typed_dict, visited=visited),
  File "/path/to/langchain_core/utils/function_calling.py", line 261, in _convert_any_typed_dicts_to_pydantic
    new_arg_type = _convert_any_typed_dicts_to_pydantic(
  File "/path/to/langchain_core/utils/function_calling.py", line 278, in _convert_any_typed_dicts_to_pydantic
    return subscriptable_origin[type_args]  # type: ignore[index]
  File "/path/to/python3.12/typing.py", line 377, in inner
    return func(*args, **kwds)
  File "/path/to/python3.12/typing.py", line 489, in __getitem__
    return self._getitem(self, parameters)
  File "/path/to/python3.12/typing.py", line 2988, in NotRequired
    item = _type_check(parameters, f'{self._name} accepts only a single type.')
  File "/path/to/python3.12/typing.py", line 204, in _type_check
    raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: NotRequired accepts only a single type. Got (<class 'str'>,).

Description

When using NotRequired fields in a TypedDict with LangChain's convert_to_openai_function() or .with_structured_output(), the conversion fails with a TypeError.

Caused by langchain_core/utils/function_calling.py (Ln 278)

return subscriptable_origin[type_args]

System Info

System Information

OS: macOS
Python Version: 3.12.0

Package Information

langchain_core: 1.0.7
langchain: 0.3.22
langsmith: 0.3.45
pydantic: 2.10.6
typing-extensions: 4.12.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelated to a bug, vulnerability, unexpected error with an existing featurecoreRelated to the package `langchain-core`langchainRelated to the package `langchain`

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions