Chainlit / docs

9 stars 60 forks source link

Minor enhancement of the LangChain integration docs #78

Open dabure opened 7 months ago

dabure commented 7 months ago

Path: /integrations/langchain

This is a suggestion for a minor improvement of the LangChain integration docs.

Environment

Problem

The below code, generates an inconvenient warning from Pylance in VS Code.

Code:

runnable = cl.user_session.get("runnable")  # type: Runnable

Warning:

Expression of type "Unknown | None" cannot be assigned to declared type "Runnable[Unknown, Unknown]" Type "Unknown | None" cannot be assigned to type "Runnable[Unknown, Unknown]" Type "None" cannot be assigned to type "Runnable[Unknown, Unknown]" Pylance(reportGeneralTypeIssues)

Pylance suggests that the code cannot guarantee that the .get("runnable") call returns a Runnable.

This is an inconvenience when copy-pasting to get started with chainlit.

Solution

I suggest you change the docs to the following, to inform the type checker that we will always receive a value with the type Runnable:

from typing import cast

# (..)

runnable = cast(Runnable, cl.user_session.get("runnable"))

# (...)

Would make a PR directly, but the repo seems to require me to create a fork, so I'm taking the lazy route and just raising an issue ;-)

Potential Limitations

  1. typing.cast was introduced in Python 3.5 together with the typing module, so if you only support Python v3.5+, this change should work for everyone. If it's correct that chainlit currently supports >=3.8.1 and <3.12, the change should be fine.

  2. Have not tested this with any other type checker.