Wrap a Runnable with additional functionality. A RunnableBinding can be thought of as a "runnable decorator" that preserves the essential features of Runnable; i.e., batching, streaming, and async support, while adding additional functionality. Any class that inherits from Runnable
| 4818 | |
| 4819 | |
| 4820 | class RunnableBinding(RunnableBindingBase[Input, Output]): |
| 4821 | """Wrap a Runnable with additional functionality. |
| 4822 | |
| 4823 | A RunnableBinding can be thought of as a "runnable decorator" that |
| 4824 | preserves the essential features of Runnable; i.e., batching, streaming, |
| 4825 | and async support, while adding additional functionality. |
| 4826 | |
| 4827 | Any class that inherits from Runnable can be bound to a `RunnableBinding`. |
| 4828 | Runnables expose a standard set of methods for creating `RunnableBindings` |
| 4829 | or sub-classes of `RunnableBindings` (e.g., `RunnableRetry`, |
| 4830 | `RunnableWithFallbacks`) that add additional functionality. |
| 4831 | |
| 4832 | These methods include: |
| 4833 | - `bind`: Bind kwargs to pass to the underlying runnable when running it. |
| 4834 | - `with_config`: Bind config to pass to the underlying runnable when running it. |
| 4835 | - `with_listeners`: Bind lifecycle listeners to the underlying runnable. |
| 4836 | - `with_types`: Override the input and output types of the underlying runnable. |
| 4837 | - `with_retry`: Bind a retry policy to the underlying runnable. |
| 4838 | - `with_fallbacks`: Bind a fallback policy to the underlying runnable. |
| 4839 | |
| 4840 | Example: |
| 4841 | |
| 4842 | `bind`: Bind kwargs to pass to the underlying runnable when running it. |
| 4843 | |
| 4844 | .. code-block:: python |
| 4845 | |
| 4846 | # Create a runnable binding that invokes the ChatModel with the |
| 4847 | # additional kwarg `stop=['-']` when running it. |
| 4848 | from langchain_community.chat_models import ChatOpenAI |
| 4849 | model = ChatOpenAI() |
| 4850 | model.invoke('Say "Parrot-MAGIC"', stop=['-']) # Should return `Parrot` |
| 4851 | # Using it the easy way via `bind` method which returns a new |
| 4852 | # RunnableBinding |
| 4853 | runnable_binding = model.bind(stop=['-']) |
| 4854 | runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot` |
| 4855 | |
| 4856 | Can also be done by instantiating a RunnableBinding directly (not recommended): |
| 4857 | |
| 4858 | .. code-block:: python |
| 4859 | |
| 4860 | from langchain_core.runnables import RunnableBinding |
| 4861 | runnable_binding = RunnableBinding( |
| 4862 | bound=model, |
| 4863 | kwargs={'stop': ['-']} # <-- Note the additional kwargs |
| 4864 | ) |
| 4865 | runnable_binding.invoke('Say "Parrot-MAGIC"') # Should return `Parrot` |
| 4866 | """ |
| 4867 | |
| 4868 | @classmethod |
| 4869 | def get_lc_namespace(cls) -> List[str]: |
| 4870 | """Get the namespace of the langchain object.""" |
| 4871 | return ["langchain", "schema", "runnable"] |
| 4872 | |
| 4873 | def bind(self, **kwargs: Any) -> Runnable[Input, Output]: |
| 4874 | """Bind additional kwargs to a Runnable, returning a new Runnable. |
| 4875 | |
| 4876 | Args: |
| 4877 | **kwargs: The kwargs to bind to the Runnable. |
no outgoing calls
no test coverage detected