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

Allow calls to batch() with 0 length arrays #10627

Merged
merged 3 commits into from Sep 15, 2023

Conversation

nfcampos
Copy link
Collaborator

This can happen if eg the input to batch is a list generated dynamically, where a 0-length list might be a valid use case

This can happen if eg the input to batch is a list generated dynamically, where a 0-length list might be a valid use case
@vercel
Copy link

vercel bot commented Sep 15, 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 Sep 15, 2023 2:26pm

@dosubot dosubot bot added the 🤖:nit Small modifications/deletions, fixes, deps or improvements to existing code or docs label Sep 15, 2023
@eyurtsev eyurtsev self-assigned this Sep 15, 2023
@@ -97,8 +97,8 @@ def get_config_list(
Helper method to get a list of configs from a single config or a list of
configs, useful for subclasses overriding batch() or abatch().
"""
if length < 1:
raise ValueError(f"length must be >= 1, but got {length}")
if length < 0:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think additional code is needed in default batch implementation since it assumes that the list of configs cannot be empty

        """
        configs = get_config_list(config, len(inputs))

        def invoke(input: Input, config: RunnableConfig) -> Union[Output, Exception]:
            if return_exceptions:
                try:
                    return self.invoke(input, config, **kwargs)
                except Exception as e:
                    return e
            else:
                return self.invoke(input, config, **kwargs)

        # If there's only one input, don't bother with the executor
        if len(inputs) == 1:
            return cast(List[Output], [invoke(inputs[0], configs[0])])

        with get_executor_for_config(configs[0]) as executor:
            return cast(List[Output], list(executor.map(invoke, inputs, configs)))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! updated

@eyurtsev
Copy link
Collaborator

eyurtsev commented Sep 15, 2023

from langchain.chat_models.openai import ChatOpenAI
llm = ChatOpenAI()
llm.batch([])

.batch([]) won't produce any traces on langsmith

@eyurtsev eyurtsev merged commit 029b2f6 into master Sep 15, 2023
31 checks passed
@eyurtsev eyurtsev deleted the nc/runnable-allow-empty-batches branch September 15, 2023 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:nit Small modifications/deletions, fixes, deps or improvements to existing code or docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants