langchain-ai/langchain

pylance type checking error when importing tool decorator

Opened this issue · 4 comments

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 read what a minimal reproducible example is (https://stackoverflow.com/help/minimal-reproducible-example).
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Example Code

The following code:

from langchain_core.tools import tool

Error Message and Stack Trace (if applicable)

Type checker pylance: strict mode) display the error message:

Type of "tool" is partially unknown
Type of "tool" is "Overload[(*, description: str | None = None, return_direct: bool = False, args_schema: type[BaseModel] | dict[str, Any] | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = "content", parse_docstring: bool = False, error_on_invalid_docstring: bool = True) -> (((((...) -> Any) | Runnable[Any, Any])) -> BaseTool), (name_or_callable: str, runnable: Runnable[Unknown, Unknown], *, description: str | None = None, return_direct: bool = False, args_schema: type[BaseModel] | dict[str, Any] | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = "content", parse_docstring: bool = False, error_on_invalid_docstring: bool = True) -> BaseTool, (name_or_callable: (...) -> Unknown, *, description: str | None = None, return_direct: bool = False, args_schema: type[BaseModel] | dict[str, Any] | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = "content", parse_docstring: bool = False, error_on_invalid_docstring: bool = True) -> BaseTool, (name_or_callable: str, *, description: str | None = None, return_direct: bool = False, args_schema: type[BaseModel] | dict[str, Any] | None = None, infer_schema: bool = True, response_format: Literal['content', 'content_and_artifact'] = "content", parse_docstring: bool = False, error_on_invalid_docstring: bool = True) -> (((((...) -> Any) | Runnable[Any, Any])) -> BaseTool)]"Pylance[reportUnknownVariableType](https://github.com/microsoft/pylance-release/blob/main/docs/diagnostics/reportUnknownVariableType.md)

Description

I want to fix the tool decorator function type hints so it does not cause a type error when I import it using strict mode.

System Info

langchain_core: 0.3.69

same even i got this issue too
maybe you can try this fix:
In file: langchain-core/src/langchain_core/tools.py

existing code:

def tool(
*args: Union[str, Callable, type[BaseModel]],
**kwargs: Any,
) -> Union[Callable, Tool]:
existing code :

Add the following line to explicitly define the type of 'tool'
tool: "typing.Any"

Hi @rishu685 I am not sure I understand, what exactly is your error ?

Not a issue its a bug same as yours!

Oh ok I have made a PR to fix this #33020 , maybe it will be merged for next versions of langchain