-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Type of issue
issue / bug
Language
Python
Description
This bug concerns the code example shown in the Functional API section of LangGraph's Quick Start guide, which you can find here.
What's the Bug?
Simply, the code example included in the docs generates errors with popular Python static checkers, like MyPy, Pylance and Ty.
The specific section of the code that generates errors is:
@entrypoint()
def agent(messages: list[BaseMessage]):
model_response = call_llm(messages).result()
while True:
if not model_response.tool_calls:
break
# Execute tools
tool_result_futures = [
call_tool(tool_call) for tool_call in model_response.tool_calls
]
tool_results = [fut.result() for fut in tool_result_futures]
messages = add_messages(messages, [model_response, *tool_results]) # Static type error!
model_response = call_llm(messages).result()
messages = add_messages(messages, model_response) # Static type error!
return messagesError Message
As shown by the code comments, the calls to the function add_messages generate the following (static type) error:
(MyPy)
Argument 1 to "add_messages" has incompatible type "list[BaseMessage]"; expected "list[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]] | BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]"Mypy[arg-type](https://mypy.readthedocs.io/en/latest/_refs.html#code-arg-type)
(Pylance)
Type "Messages" is not assignable to declared type "list[BaseMessage]"
Type "Messages" is not assignable to type "list[BaseMessage]"
"BaseMessage" is not assignable to "list[BaseMessage]"
(ty)
Object of type `list[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]] | BaseMessage | list[str] | ... omitted 3 union elements` is not assignable to `list[BaseMessage]`: Incompatible value of type `list[BaseMessage | list[str] | tuple[str, str] | str | dict[str, Any]] | BaseMessage | list[str] | ... omitted 3 union elements`
Related Issue
This is likely related to a root issue within the LangGraph framework. A currently active pull request mentions a similar issue, except that the changes suggested do not solve the type errors.
What Must Be Changed?
Since the code example works, the easiest solution is to add #type: ignore to the lines specified above. However, as you might guess, this solution is not clean, and it doesn't help that LangChain started an initiative last year to remove #type: ignore comments from the codebase.
A softer solution is to have the docs inform users that they can "Safely ignore any errors generated from static type checkers".