Skip to content
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

Merged
merged 2 commits into from
Oct 31, 2023

Conversation

nfcampos
Copy link
Collaborator

@nfcampos nfcampos commented Oct 31, 2023

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

@vercel
Copy link

vercel bot commented Oct 31, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Oct 31, 2023 11:08am

@dosubot dosubot bot added the 🤖:improvement Medium size change to existing code to handle new use-cases label Oct 31, 2023
…t type of 2nd item in sequence - output type of runnable assign"
@nfcampos nfcampos requested a review from eyurtsev October 31, 2023 11:10
Copy link
Collaborator

@hinthornw hinthornw left a 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

@nfcampos nfcampos merged commit 3143324 into master Oct 31, 2023
@nfcampos nfcampos deleted the nc/31oct/runnable-type-inference branch October 31, 2023 13:22
from langchain.schema.runnable.passthrough import RunnableAssign

if isinstance(self.first, RunnableAssign):
first = cast(RunnableAssign, self.first)
Copy link
Collaborator

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.

Copy link
Collaborator Author

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)

Copy link
Collaborator

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.

Copy link
Collaborator

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 :)

xieqihui pushed a commit to xieqihui/langchain that referenced this pull request Nov 21, 2023
- 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"
hoanq1811 pushed a commit to hoanq1811/langchain that referenced this pull request Feb 2, 2024
- 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants