Assigns new fields to the dict output of this runnable. Returns a new runnable. .. code-block:: python from langchain_community.llms.fake import FakeStreamingListLLM from langchain_core.output_parsers import StrOutputParser from langchain_core.pr
(
self,
**kwargs: Union[
Runnable[Dict[str, Any], Any],
Callable[[Dict[str, Any]], Any],
Mapping[
str,
Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any]],
],
],
)
| 531 | return self | RunnablePick(keys) |
| 532 | |
| 533 | def assign( |
| 534 | self, |
| 535 | **kwargs: Union[ |
| 536 | Runnable[Dict[str, Any], Any], |
| 537 | Callable[[Dict[str, Any]], Any], |
| 538 | Mapping[ |
| 539 | str, |
| 540 | Union[Runnable[Dict[str, Any], Any], Callable[[Dict[str, Any]], Any]], |
| 541 | ], |
| 542 | ], |
| 543 | ) -> RunnableSerializable[Any, Any]: |
| 544 | """Assigns new fields to the dict output of this runnable. |
| 545 | Returns a new runnable. |
| 546 | |
| 547 | .. code-block:: python |
| 548 | |
| 549 | from langchain_community.llms.fake import FakeStreamingListLLM |
| 550 | from langchain_core.output_parsers import StrOutputParser |
| 551 | from langchain_core.prompts import SystemMessagePromptTemplate |
| 552 | from langchain_core.runnables import Runnable |
| 553 | from operator import itemgetter |
| 554 | |
| 555 | prompt = ( |
| 556 | SystemMessagePromptTemplate.from_template("You are a nice assistant.") |
| 557 | + "{question}" |
| 558 | ) |
| 559 | llm = FakeStreamingListLLM(responses=["foo-lish"]) |
| 560 | |
| 561 | chain: Runnable = prompt | llm | {"str": StrOutputParser()} |
| 562 | |
| 563 | chain_with_assign = chain.assign(hello=itemgetter("str") | llm) |
| 564 | |
| 565 | print(chain_with_assign.input_schema.schema()) |
| 566 | # {'title': 'PromptInput', 'type': 'object', 'properties': |
| 567 | {'question': {'title': 'Question', 'type': 'string'}}} |
| 568 | print(chain_with_assign.output_schema.schema()) # |
| 569 | {'title': 'RunnableSequenceOutput', 'type': 'object', 'properties': |
| 570 | {'str': {'title': 'Str', |
| 571 | 'type': 'string'}, 'hello': {'title': 'Hello', 'type': 'string'}}} |
| 572 | |
| 573 | """ |
| 574 | from langchain_core.runnables.passthrough import RunnableAssign |
| 575 | |
| 576 | return self | RunnableAssign(RunnableParallel(kwargs)) |
| 577 | |
| 578 | """ --- Public API --- """ |
| 579 |