docs: Add in code documentation to core Runnable with_fallbacks method (docs only) (#19104)

- Description: [a description of the change] Add in code documentation
to core Runnable with_fallbacks method (docs only)
- Issue: the issue #18804 
@eyurtsev PTAL
This commit is contained in:
Guangdong Liu 2024-03-16 06:21:10 +08:00 committed by GitHub
parent cced3eb9bc
commit 4468e5bdbe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1380,6 +1380,29 @@ class Runnable(Generic[Input, Output], ABC):
) -> RunnableWithFallbacksT[Input, Output]:
"""Add fallbacks to a runnable, returning a new Runnable.
Example:
.. code-block:: python
from typing import Iterator
from langchain_core.runnables import RunnableGenerator
def _generate_immediate_error(input: Iterator) -> Iterator[str]:
raise ValueError()
yield ""
def _generate(input: Iterator) -> Iterator[str]:
yield from "foo bar"
runnable = RunnableGenerator(_generate_immediate_error).with_fallbacks(
[RunnableGenerator(_generate)]
)
print(''.join(runnable.stream({}))) #foo bar
Args:
fallbacks: A sequence of runnables to try if the original runnable fails.
exceptions_to_handle: A tuple of exception types to handle.
@ -1391,6 +1414,7 @@ class Runnable(Generic[Input, Output], ABC):
Returns:
A new Runnable that will try the original runnable, and then each
fallback in order, upon failures.
"""
from langchain_core.runnables.fallbacks import RunnableWithFallbacks