-
Notifications
You must be signed in to change notification settings - Fork 16.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve Runnable type inference for input_schemas #12630
Conversation
nfcampos
commented
Oct 31, 2023
•
edited
Loading
edited
- Prefer lambda type annotations over inferred dict schema
- For sequences that start with RunnableAssign infer seq input type as "input type of 2nd item in sequence - output type of runnable assign"
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
…t type of 2nd item in sequence - output type of runnable assign"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome this fixes my issue
from langchain.schema.runnable.passthrough import RunnableAssign | ||
|
||
if isinstance(self.first, RunnableAssign): | ||
first = cast(RunnableAssign, self.first) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mypy supports type narrowing, which makes the cast
here a no-op. Narrowing here means that it sees that it's inside an if
whose condition ensures self.first
's type is RunnableAssign
, and for the rest of the block it knows that self.first
stays RunnableAssign
even though outside the block it could be something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason mypy was inferring this here as Never
. Maybe because this is a bit circular (thus the import inside the function)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, that's wild. Sounds like a bug.
Never
might have been appropriate if Runnable
and RunnableAssign
didn't have any classes in common (i.e. could not both be true), but RunnableAssign
is a Runnable
already.
We're unfortunately running a year-old version of mypy (0.9xx instead of 1.6.1 which is latest) so I don't think we can report this or get help on it until we upgrade. It's possible that upgrading gets us bug fixes for stuff like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might also be good to leave a note about why this cast is appropriate, since otherwise at least to my eyes it seems out of place. Put a sign up on Chesterton's fence describing why it's there, in a sense :)
- Prefer lambda type annotations over inferred dict schema - For sequences that start with RunnableAssign infer seq input type as "input type of 2nd item in sequence - output type of runnable assign"
- Prefer lambda type annotations over inferred dict schema - For sequences that start with RunnableAssign infer seq input type as "input type of 2nd item in sequence - output type of runnable assign"