1import asyncio2import json3import re4import sys5import time6import uuid7import warnings8from collections.abc import (9 AsyncIterator,10 Awaitable,11 Callable,12 Iterator,13 Sequence,14)15from functools import partial16from operator import itemgetter17from typing import TYPE_CHECKING, Any, cast18from uuid import UUID1920import pytest21from freezegun import freeze_time22from packaging import version23from pydantic import BaseModel, Field, ValidationError24from pydantic.v1 import BaseModel as BaseModelV125from pydantic.v1 import Field as FieldV126from pydantic.v1 import ValidationError as ValidationErrorV127from pytest_mock import MockerFixture28from syrupy.assertion import SnapshotAssertion29from typing_extensions import TypedDict, override3031from langchain_core.callbacks import BaseCallbackHandler32from langchain_core.callbacks.manager import (33 AsyncCallbackManagerForRetrieverRun,34 CallbackManagerForLLMRun,35 CallbackManagerForRetrieverRun,36 atrace_as_chain_group,37 trace_as_chain_group,38)39from langchain_core.documents import Document40from langchain_core.language_models import (41 BaseChatModel,42 FakeListChatModel,43 FakeListLLM,44 FakeStreamingListLLM,45)46from langchain_core.language_models.fake_chat_models import GenericFakeChatModel47from langchain_core.load import dumpd, dumps48from langchain_core.load.load import loads49from langchain_core.messages import AIMessageChunk, HumanMessage, SystemMessage50from langchain_core.messages.base import BaseMessage51from langchain_core.messages.tool import tool_call_chunk52from langchain_core.output_parsers import (53 BaseOutputParser,54 CommaSeparatedListOutputParser,55 StrOutputParser,56)57from langchain_core.outputs.chat_generation import ChatGeneration, ChatGenerationChunk58from langchain_core.outputs.chat_result import ChatResult59from langchain_core.outputs.llm_result import LLMResult60from langchain_core.prompt_values import ChatPromptValue, StringPromptValue61from langchain_core.prompts import (62 ChatPromptTemplate,63 HumanMessagePromptTemplate,64 MessagesPlaceholder,65 PromptTemplate,66 SystemMessagePromptTemplate,67)68from langchain_core.retrievers import BaseRetriever69from langchain_core.runnables import (70 AddableDict,71 ConfigurableField,72 ConfigurableFieldMultiOption,73 ConfigurableFieldSingleOption,74 RouterRunnable,75 Runnable,76 RunnableAssign,77 RunnableBinding,78 RunnableBranch,79 RunnableConfig,80 RunnableGenerator,81 RunnableLambda,82 RunnableParallel,83 RunnablePassthrough,84 RunnablePick,85 RunnableSequence,86 add,87 chain,88)89from langchain_core.runnables.base import (90 RunnableMap,91 RunnableSerializable,92 _ChunkAccumulator,93)94from langchain_core.runnables.utils import Input, Output95from langchain_core.tools import BaseTool, tool96from langchain_core.tracers import (97 BaseTracer,98 ConsoleCallbackHandler,99 Run,100 RunLog,101 RunLogPatch,102)103from langchain_core.tracers._compat import pydantic_copy104from langchain_core.tracers.context import collect_runs105from langchain_core.utils.json import parse_partial_json106from langchain_core.utils.pydantic import (107 PYDANTIC_VERSION,108 TypeBaseModel,109 model_validate,110)111from langchain_core.version import VERSION112from tests.unit_tests.pydantic_utils import (113 _normalize_schema,114 _schema,115 skip_if_no_pydantic_v1,116)117from tests.unit_tests.stubs import AnyStr, _any_id_ai_message, _any_id_ai_message_chunk118119if TYPE_CHECKING:120 from langchain_core.messages.ai import InputTokenDetails, UsageMetadata121122# Several tests assert the legacy `RunLog` / `RunLogPatch` output produced by123# `astream_log`, which cannot be replaced by `astream` without losing coverage.124pytestmark = pytest.mark.filterwarnings(125 "ignore:astream_log is deprecated. Use astream instead.:"126 "langchain_core._api.deprecation.LangChainDeprecationWarning"127)128129PYDANTIC_VERSION_AT_LEAST_29 = version.parse("2.9") <= PYDANTIC_VERSION130PYDANTIC_VERSION_AT_LEAST_210 = version.parse("2.10") <= PYDANTIC_VERSION131132133def _normalize_lc_version(value: str) -> str:134 return value.replace(135 f"'langchain-core': '{VERSION}'",136 "'langchain-core': '<version>'",137 )138139140class FakeTracer(BaseTracer):141 """Fake tracer that records LangChain execution.142143 It replaces run IDs with deterministic UUIDs for snapshotting.144 """145146 def __init__(self) -> None:147 """Initialize the tracer."""148 super().__init__()149 self.runs: list[Run] = []150 self.uuids_map: dict[UUID, UUID] = {}151 self.uuids_generator = (152 UUID(f"00000000-0000-4000-8000-{i:012}", version=4) for i in range(10000)153 )154155 def _replace_uuid(self, uuid: UUID) -> UUID:156 if uuid not in self.uuids_map:157 self.uuids_map[uuid] = next(self.uuids_generator)158 return self.uuids_map[uuid]159160 def _replace_message_id(self, maybe_message: Any) -> Any:161 if isinstance(maybe_message, BaseMessage):162 maybe_message.id = str(next(self.uuids_generator))163 if isinstance(maybe_message, ChatGeneration):164 maybe_message.message.id = str(next(self.uuids_generator))165 if isinstance(maybe_message, LLMResult):166 for i, gen_list in enumerate(maybe_message.generations):167 for j, gen in enumerate(gen_list):168 maybe_message.generations[i][j] = self._replace_message_id(gen)169 if isinstance(maybe_message, dict):170 for k, v in maybe_message.items():171 maybe_message[k] = self._replace_message_id(v)172 if isinstance(maybe_message, list):173 for i, v in enumerate(maybe_message):174 maybe_message[i] = self._replace_message_id(v)175176 return maybe_message177178 def _copy_run(self, run: Run) -> Run:179 if run.dotted_order:180 levels = run.dotted_order.split(".")181 processed_levels = []182 for level in levels:183 timestamp, run_id = level.split("Z")184 new_run_id = self._replace_uuid(UUID(run_id))185 processed_level = f"{timestamp}Z{new_run_id}"186 processed_levels.append(processed_level)187 new_dotted_order = ".".join(processed_levels)188 else:189 new_dotted_order = None190 update_dict = {191 "id": self._replace_uuid(run.id),192 "parent_run_id": (193 self.uuids_map[run.parent_run_id] if run.parent_run_id else None194 ),195 "child_runs": [self._copy_run(child) for child in run.child_runs],196 "trace_id": self._replace_uuid(run.trace_id) if run.trace_id else None,197 "dotted_order": new_dotted_order,198 "inputs": self._replace_message_id(run.inputs),199 "outputs": self._replace_message_id(run.outputs),200 }201 return pydantic_copy(run, update=update_dict)202203 def _persist_run(self, run: Run) -> None:204 """Persist a run."""205 self.runs.append(self._copy_run(run))206207 def flattened_runs(self) -> list[Run]:208 q = [*self.runs]209 result = []210 while q:211 parent = q.pop()212 result.append(parent)213 if parent.child_runs:214 q.extend(parent.child_runs)215 return result216217 @property218 def run_ids(self) -> list[uuid.UUID | None]:219 runs = self.flattened_runs()220 uuids_map = {v: k for k, v in self.uuids_map.items()}221 return [uuids_map.get(r.id) for r in runs]222223224class FakeRunnable(Runnable[str, int]):225 @override226 def invoke(227 self,228 input: str,229 config: RunnableConfig | None = None,230 **kwargs: Any,231 ) -> int:232 return len(input)233234235class FakeRunnableSerializable(RunnableSerializable[str, int]):236 hello: str = ""237238 @override239 def invoke(240 self,241 input: str,242 config: RunnableConfig | None = None,243 **kwargs: Any,244 ) -> int:245 return len(input)246247248class FakeRetriever(BaseRetriever):249 @override250 def _get_relevant_documents(251 self, query: str, *, run_manager: CallbackManagerForRetrieverRun252 ) -> list[Document]:253 return [Document(page_content="foo"), Document(page_content="bar")]254255 @override256 async def _aget_relevant_documents(257 self, query: str, *, run_manager: AsyncCallbackManagerForRetrieverRun258 ) -> list[Document]:259 return [Document(page_content="foo"), Document(page_content="bar")]260261262@pytest.mark.skipif(263 PYDANTIC_VERSION_AT_LEAST_210,264 reason=(265 "Only test with most recent version of pydantic. "266 "Pydantic introduced small fixes to generated JSONSchema on minor versions."267 ),268)269def test_schemas(snapshot: SnapshotAssertion) -> None:270 fake = FakeRunnable() # str -> int271272 assert fake.get_input_jsonschema() == {273 "title": "FakeRunnableInput",274 "type": "string",275 }276 assert fake.get_output_jsonschema() == {277 "title": "FakeRunnableOutput",278 "type": "integer",279 }280 assert fake.get_config_jsonschema(include=["tags", "metadata", "run_name"]) == {281 "properties": {282 "metadata": {283 "default": None,284 "title": "Metadata",285 "type": "object",286 },287 "run_name": {"default": None, "title": "Run Name", "type": "string"},288 "tags": {289 "default": None,290 "items": {"type": "string"},291 "title": "Tags",292 "type": "array",293 },294 },295 "title": "FakeRunnableConfig",296 "type": "object",297 }298299 fake_bound = FakeRunnable().bind(a="b") # str -> int300301 assert fake_bound.get_input_jsonschema() == {302 "title": "FakeRunnableInput",303 "type": "string",304 }305 assert fake_bound.get_output_jsonschema() == {306 "title": "FakeRunnableOutput",307 "type": "integer",308 }309310 fake_w_fallbacks = FakeRunnable().with_fallbacks((fake,)) # str -> int311312 assert fake_w_fallbacks.get_input_jsonschema() == {313 "title": "FakeRunnableInput",314 "type": "string",315 }316 assert fake_w_fallbacks.get_output_jsonschema() == {317 "title": "FakeRunnableOutput",318 "type": "integer",319 }320321 def typed_lambda_impl(x: str) -> int:322 return len(x)323324 typed_lambda = RunnableLambda(typed_lambda_impl) # str -> int325326 assert typed_lambda.get_input_jsonschema() == {327 "title": "typed_lambda_impl_input",328 "type": "string",329 }330 assert typed_lambda.get_output_jsonschema() == {331 "title": "typed_lambda_impl_output",332 "type": "integer",333 }334335 async def typed_async_lambda_impl(x: str) -> int:336 return len(x)337338 typed_async_lambda = RunnableLambda(typed_async_lambda_impl) # str -> int339340 assert typed_async_lambda.get_input_jsonschema() == {341 "title": "typed_async_lambda_impl_input",342 "type": "string",343 }344 assert typed_async_lambda.get_output_jsonschema() == {345 "title": "typed_async_lambda_impl_output",346 "type": "integer",347 }348349 fake_ret = FakeRetriever() # str -> list[Document]350351 assert fake_ret.get_input_jsonschema() == {352 "title": "FakeRetrieverInput",353 "type": "string",354 }355 assert _normalize_schema(fake_ret.get_output_jsonschema()) == {356 "$defs": {357 "Document": {358 "description": "Class for storing a piece of text and "359 "associated metadata.\n"360 "\n"361 "!!! note\n"362 "\n"363 " `Document` is for **retrieval workflows**, not chat I/O. For "364 "sending text\n"365 " to an LLM in a conversation, use message types from "366 "`langchain.messages`.\n"367 "\n"368 "Example:\n"369 " ```python\n"370 " from langchain_core.documents import Document\n"371 "\n"372 " document = Document(\n"373 ' page_content="Hello, world!", '374 'metadata={"source": "https://example.com"}\n'375 " )\n"376 " ```",377 "properties": {378 "id": {379 "anyOf": [{"type": "string"}, {"type": "null"}],380 "default": None,381 "title": "Id",382 },383 "metadata": {"title": "Metadata", "type": "object"},384 "page_content": {"title": "Page Content", "type": "string"},385 "type": {386 "const": "Document",387 "default": "Document",388 "title": "Type",389 },390 },391 "required": ["page_content"],392 "title": "Document",393 "type": "object",394 }395 },396 "items": {"$ref": "#/$defs/Document"},397 "title": "FakeRetrieverOutput",398 "type": "array",399 }400401 fake_llm = FakeListLLM(responses=["a"]) # str -> list[list[str]]402403 assert _schema(fake_llm.input_schema) == snapshot(name="fake_llm_input_schema")404 assert _schema(fake_llm.output_schema) == {405 "title": "FakeListLLMOutput",406 "type": "string",407 }408409 fake_chat = FakeListChatModel(responses=["a"]) # str -> list[list[str]]410411 assert _schema(fake_chat.input_schema) == snapshot(name="fake_chat_input_schema")412 assert _schema(fake_chat.output_schema) == snapshot(name="fake_chat_output_schema")413414 chat_prompt = ChatPromptTemplate.from_messages(415 [416 MessagesPlaceholder(variable_name="history"),417 ("human", "Hello, how are you?"),418 ]419 )420421 assert _normalize_schema(chat_prompt.get_input_jsonschema()) == snapshot(422 name="chat_prompt_input_schema"423 )424 assert _normalize_schema(chat_prompt.get_output_jsonschema()) == snapshot(425 name="chat_prompt_output_schema"426 )427428 prompt = PromptTemplate.from_template("Hello, {name}!")429430 assert prompt.get_input_jsonschema() == {431 "title": "PromptInput",432 "type": "object",433 "properties": {"name": {"title": "Name", "type": "string"}},434 "required": ["name"],435 }436 assert _schema(prompt.output_schema) == snapshot(name="prompt_output_schema")437438 prompt_mapper = PromptTemplate.from_template("Hello, {name}!").map()439440 assert _normalize_schema(prompt_mapper.get_input_jsonschema()) == {441 "$defs": {442 "PromptInput": {443 "properties": {"name": {"title": "Name", "type": "string"}},444 "required": ["name"],445 "title": "PromptInput",446 "type": "object",447 }448 },449 "default": None,450 "items": {"$ref": "#/$defs/PromptInput"},451 "title": "RunnableEach<PromptTemplate>Input",452 "type": "array",453 }454 assert _schema(prompt_mapper.output_schema) == snapshot(455 name="prompt_mapper_output_schema"456 )457458 list_parser = CommaSeparatedListOutputParser()459460 assert _schema(list_parser.input_schema) == snapshot(461 name="list_parser_input_schema"462 )463 assert _schema(list_parser.output_schema) == {464 "title": "CommaSeparatedListOutputParserOutput",465 "type": "array",466 "items": {"type": "string"},467 }468469 seq = prompt | fake_llm | list_parser470471 assert seq.get_input_jsonschema() == {472 "title": "PromptInput",473 "type": "object",474 "properties": {"name": {"title": "Name", "type": "string"}},475 "required": ["name"],476 }477 assert seq.get_output_jsonschema() == {478 "type": "array",479 "items": {"type": "string"},480 "title": "CommaSeparatedListOutputParserOutput",481 }482483 router = RouterRunnable[Any]({})484485 assert _schema(router.input_schema) == {486 "$ref": "#/definitions/RouterInput",487 "definitions": {488 "RouterInput": {489 "description": "Router input.",490 "properties": {491 "input": {"title": "Input"},492 "key": {"title": "Key", "type": "string"},493 },494 "required": ["key", "input"],495 "title": "RouterInput",496 "type": "object",497 }498 },499 "title": "RouterRunnableInput",500 }501 assert router.get_output_jsonschema() == {"title": "RouterRunnableOutput"}502503 seq_w_map = (504 prompt505 | fake_llm506 | {507 "original": RunnablePassthrough(input_type=str),508 "as_list": list_parser,509 "length": typed_lambda_impl,510 }511 )512513 assert seq_w_map.get_input_jsonschema() == {514 "title": "PromptInput",515 "type": "object",516 "properties": {"name": {"title": "Name", "type": "string"}},517 "required": ["name"],518 }519 assert seq_w_map.get_output_jsonschema() == {520 "title": "RunnableParallel<original,as_list,length>Output",521 "type": "object",522 "properties": {523 "original": {"title": "Original", "type": "string"},524 "length": {"title": "Length", "type": "integer"},525 "as_list": {526 "title": "As List",527 "type": "array",528 "items": {"type": "string"},529 },530 },531 "required": ["original", "as_list", "length"],532 }533534 # Add a test for schema of runnable assign535 def foo(x: int) -> int:536 return x537538 foo_ = RunnableLambda(foo)539540 assert foo_.assign(bar=lambda _: "foo").get_output_jsonschema() == {541 "properties": {"bar": {"title": "Bar"}, "root": {"title": "Root"}},542 "required": ["root", "bar"],543 "title": "RunnableAssignOutput",544 "type": "object",545 }546547548def test_passthrough_assign_schema() -> None:549 retriever = FakeRetriever() # str -> list[Document]550 prompt = PromptTemplate.from_template("{context} {question}")551 fake_llm = FakeListLLM(responses=["a"]) # str -> list[list[str]]552553 seq_w_assign = (554 RunnablePassthrough.assign(context=itemgetter("question") | retriever)555 | prompt556 | fake_llm557 )558559 assert seq_w_assign.get_input_jsonschema() == {560 "properties": {"question": {"title": "Question", "type": "string"}},561 "title": "RunnableSequenceInput",562 "type": "object",563 "required": ["question"],564 }565 assert seq_w_assign.get_output_jsonschema() == {566 "title": "FakeListLLMOutput",567 "type": "string",568 }569570 invalid_seq_w_assign = (571 RunnablePassthrough.assign(context=itemgetter("question") | retriever)572 | fake_llm # type: ignore[operator]573 )574575 # fallback to RunnableAssign.input_schema if next runnable doesn't have576 # expected dict input_schema577 assert invalid_seq_w_assign.get_input_jsonschema() == {578 "properties": {"question": {"title": "Question"}},579 "title": "RunnableParallel<context>Input",580 "type": "object",581 "required": ["question"],582 }583584585def test_lambda_schemas(snapshot: SnapshotAssertion) -> None:586 first_lambda = lambda x: x["hello"] # noqa: E731587 assert RunnableLambda(first_lambda).get_input_jsonschema() == {588 "title": "RunnableLambdaInput",589 "type": "object",590 "properties": {"hello": {"title": "Hello"}},591 "required": ["hello"],592 }593594 second_lambda = lambda x, y: (x["hello"], x["bye"], y["bah"]) # noqa: E731595 assert RunnableLambda(second_lambda).get_input_jsonschema() == {596 "title": "RunnableLambdaInput",597 "type": "object",598 "properties": {"hello": {"title": "Hello"}, "bye": {"title": "Bye"}},599 "required": ["bye", "hello"],600 }601602 def get_value(value): # type: ignore[no-untyped-def] # noqa: ANN001,ANN202603 return value["variable_name"]604605 assert RunnableLambda(get_value).get_input_jsonschema() == {606 "title": "get_value_input",607 "type": "object",608 "properties": {"variable_name": {"title": "Variable Name"}},609 "required": ["variable_name"],610 }611612 async def aget_value(value): # type: ignore[no-untyped-def] # noqa: ANN001,ANN202613 return (value["variable_name"], value.get("another"))614615 assert RunnableLambda(aget_value).get_input_jsonschema() == {616 "title": "aget_value_input",617 "type": "object",618 "properties": {619 "another": {"title": "Another"},620 "variable_name": {"title": "Variable Name"},621 },622 "required": ["another", "variable_name"],623 }624625 async def aget_values(value): # type: ignore[no-untyped-def] # noqa: ANN001,ANN202626 return {627 "hello": value["variable_name"],628 "bye": value["variable_name"],629 "byebye": value["yo"],630 }631632 assert RunnableLambda(aget_values).get_input_jsonschema() == {633 "title": "aget_values_input",634 "type": "object",635 "properties": {636 "variable_name": {"title": "Variable Name"},637 "yo": {"title": "Yo"},638 },639 "required": ["variable_name", "yo"],640 }641642 class InputType(TypedDict):643 variable_name: str644 yo: int645646 class OutputType(TypedDict):647 hello: str648 bye: str649 byebye: int650651 async def aget_values_typed(value: InputType) -> OutputType:652 return {653 "hello": value["variable_name"],654 "bye": value["variable_name"],655 "byebye": value["yo"],656 }657658 assert _normalize_schema(659 RunnableLambda(aget_values_typed).get_input_jsonschema()660 ) == _normalize_schema(661 {662 "$defs": {663 "InputType": {664 "properties": {665 "variable_name": {666 "title": "Variable Name",667 "type": "string",668 },669 "yo": {"title": "Yo", "type": "integer"},670 },671 "required": ["variable_name", "yo"],672 "title": "InputType",673 "type": "object",674 }675 },676 "allOf": [{"$ref": "#/$defs/InputType"}],677 "title": "aget_values_typed_input",678 }679 )680681 if PYDANTIC_VERSION_AT_LEAST_29:682 assert _normalize_schema(683 RunnableLambda(aget_values_typed).get_output_jsonschema()684 ) == snapshot(name="schema8")685686687def test_with_types_with_type_generics() -> None:688 """Verify that with_types works if we use things like list[int]."""689690 def foo(x: int) -> None:691 """Add one to the input."""692 raise NotImplementedError693694 # Try specifying some695 RunnableLambda(foo).with_types(696 output_type=list[int], # type: ignore[arg-type]697 input_type=list[int], # type: ignore[arg-type]698 )699 RunnableLambda(foo).with_types(700 output_type=Sequence[int], # type: ignore[arg-type]701 input_type=Sequence[int], # type: ignore[arg-type]702 )703704705def test_schema_with_itemgetter() -> None:706 """Test runnable with itemgetter."""707 foo = RunnableLambda(itemgetter("hello"))708 assert _schema(foo.input_schema) == {709 "properties": {"hello": {"title": "Hello"}},710 "required": ["hello"],711 "title": "RunnableLambdaInput",712 "type": "object",713 }714 prompt = ChatPromptTemplate.from_template("what is {language}?")715 chain = {"language": itemgetter("language")} | prompt716 assert _schema(chain.input_schema) == {717 "properties": {"language": {"title": "Language"}},718 "required": ["language"],719 "title": "RunnableParallel<language>Input",720 "type": "object",721 }722723724def test_schema_complex_seq() -> None:725 prompt1 = ChatPromptTemplate.from_template("what is the city {person} is from?")726 prompt2 = ChatPromptTemplate.from_template(727 "what country is the city {city} in? respond in {language}"728 )729730 model = FakeListChatModel(responses=[""])731732 chain1 = RunnableSequence[dict[str, Any], str](733 prompt1, model, StrOutputParser(), name="city_chain"734 )735736 assert chain1.name == "city_chain"737738 chain2 = (739 {"city": chain1, "language": itemgetter("language")}740 | prompt2741 | model742 | StrOutputParser()743 )744745 assert chain2.get_input_jsonschema() == {746 "title": "RunnableParallel<city,language>Input",747 "type": "object",748 "properties": {749 "person": {"title": "Person", "type": "string"},750 "language": {"title": "Language"},751 },752 "required": ["person", "language"],753 }754755 assert chain2.get_output_jsonschema() == {756 "title": "StrOutputParserOutput",757 "type": "string",758 }759760 assert chain2.with_types(input_type=str).get_input_jsonschema() == {761 "title": "RunnableSequenceInput",762 "type": "string",763 }764765 assert chain2.with_types(input_type=int).get_output_jsonschema() == {766 "title": "StrOutputParserOutput",767 "type": "string",768 }769770 class InputType(BaseModel):771 person: str772773 assert chain2.with_types(input_type=InputType).get_input_jsonschema() == {774 "title": "InputType",775 "type": "object",776 "properties": {"person": {"title": "Person", "type": "string"}},777 "required": ["person"],778 }779780781def test_configurable_fields(snapshot: SnapshotAssertion) -> None:782 fake_llm = FakeListLLM(responses=["a"]) # str -> list[list[str]]783784 assert fake_llm.invoke("...") == "a"785786 fake_llm_configurable = fake_llm.configurable_fields(787 responses=ConfigurableField(788 id="llm_responses",789 name="LLM Responses",790 description="A list of fake responses for this LLM",791 )792 )793794 assert fake_llm_configurable.invoke("...") == "a"795796 if PYDANTIC_VERSION_AT_LEAST_29:797 assert _normalize_schema(798 fake_llm_configurable.get_config_jsonschema()799 ) == snapshot(name="schema2")800801 fake_llm_configured = fake_llm_configurable.with_config(802 configurable={"llm_responses": ["b"]}803 )804805 assert fake_llm_configured.invoke("...") == "b"806807 prompt = PromptTemplate.from_template("Hello, {name}!")808809 assert prompt.invoke({"name": "John"}) == StringPromptValue(text="Hello, John!")810811 prompt_configurable = prompt.configurable_fields(812 template=ConfigurableField(813 id="prompt_template",814 name="Prompt Template",815 description="The prompt template for this chain",816 )817 )818819 assert prompt_configurable.invoke({"name": "John"}) == StringPromptValue(820 text="Hello, John!"821 )822823 if PYDANTIC_VERSION_AT_LEAST_29:824 assert _normalize_schema(825 prompt_configurable.get_config_jsonschema()826 ) == snapshot(name="schema3")827828 prompt_configured = prompt_configurable.with_config(829 configurable={"prompt_template": "Hello, {name}! {name}!"}830 )831832 assert prompt_configured.invoke({"name": "John"}) == StringPromptValue(833 text="Hello, John! John!"834 )835836 assert prompt_configurable.with_config(837 configurable={"prompt_template": "Hello {name} in {lang}"}838 ).get_input_jsonschema() == {839 "title": "PromptInput",840 "type": "object",841 "properties": {842 "lang": {"title": "Lang", "type": "string"},843 "name": {"title": "Name", "type": "string"},844 },845 "required": ["lang", "name"],846 }847848 chain_configurable = prompt_configurable | fake_llm_configurable | StrOutputParser()849850 assert chain_configurable.invoke({"name": "John"}) == "a"851852 if PYDANTIC_VERSION_AT_LEAST_29:853 assert _normalize_schema(854 chain_configurable.get_config_jsonschema()855 ) == snapshot(name="schema4")856857 assert (858 chain_configurable.with_config(859 configurable={860 "prompt_template": "A very good morning to you, {name} {lang}!",861 "llm_responses": ["c"],862 }863 ).invoke({"name": "John", "lang": "en"})864 == "c"865 )866867 assert chain_configurable.with_config(868 configurable={869 "prompt_template": "A very good morning to you, {name} {lang}!",870 "llm_responses": ["c"],871 }872 ).get_input_jsonschema() == {873 "title": "PromptInput",874 "type": "object",875 "properties": {876 "lang": {"title": "Lang", "type": "string"},877 "name": {"title": "Name", "type": "string"},878 },879 "required": ["lang", "name"],880 }881882 chain_with_map_configurable = prompt_configurable | {883 "llm1": fake_llm_configurable | StrOutputParser(),884 "llm2": fake_llm_configurable | StrOutputParser(),885 "llm3": fake_llm.configurable_fields(886 responses=ConfigurableField("other_responses")887 )888 | StrOutputParser(),889 }890891 assert chain_with_map_configurable.invoke({"name": "John"}) == {892 "llm1": "a",893 "llm2": "a",894 "llm3": "a",895 }896897 if PYDANTIC_VERSION_AT_LEAST_29:898 assert _normalize_schema(899 chain_with_map_configurable.get_config_jsonschema()900 ) == snapshot(name="schema5")901902 assert chain_with_map_configurable.with_config(903 configurable={904 "prompt_template": "A very good morning to you, {name}!",905 "llm_responses": ["c"],906 "other_responses": ["d"],907 }908 ).invoke({"name": "John"}) == {"llm1": "c", "llm2": "c", "llm3": "d"}909910911def test_configurable_alts_factory() -> None:912 fake_llm = FakeListLLM(responses=["a"]).configurable_alternatives(913 ConfigurableField(id="llm", name="LLM"),914 chat=partial(FakeListLLM, responses=["b"]),915 )916917 assert fake_llm.invoke("...") == "a"918919 assert fake_llm.with_config(configurable={"llm": "chat"}).invoke("...") == "b"920921922def test_configurable_fields_prefix_keys(snapshot: SnapshotAssertion) -> None:923 fake_chat = FakeListChatModel(responses=["b"]).configurable_fields(924 responses=ConfigurableFieldMultiOption(925 id="responses",926 name="Chat Responses",927 options={928 "hello": "A good morning to you!",929 "bye": "See you later!",930 "helpful": "How can I help you?",931 },932 default=["hello", "bye"],933 ),934 # (sleep is a configurable field in FakeListChatModel)935 sleep=ConfigurableField(936 id="chat_sleep",937 is_shared=True,938 ),939 )940 fake_llm = (941 FakeListLLM(responses=["a"])942 .configurable_fields(943 responses=ConfigurableField(944 id="responses",945 name="LLM Responses",946 description="A list of fake responses for this LLM",947 )948 )949 .configurable_alternatives(950 ConfigurableField(id="llm", name="LLM"),951 chat=fake_chat | StrOutputParser(),952 prefix_keys=True,953 )954 )955 prompt = PromptTemplate.from_template("Hello, {name}!").configurable_fields(956 template=ConfigurableFieldSingleOption(957 id="prompt_template",958 name="Prompt Template",959 description="The prompt template for this chain",960 options={961 "hello": "Hello, {name}!",962 "good_morning": "A very good morning to you, {name}!",963 },964 default="hello",965 )966 )967968 chain = prompt | fake_llm969970 if PYDANTIC_VERSION_AT_LEAST_29:971 assert _normalize_schema(_schema(chain.config_schema())) == snapshot(972 name="schema6"973 )974975976def test_configurable_fields_example(snapshot: SnapshotAssertion) -> None:977 fake_chat = FakeListChatModel(responses=["b"]).configurable_fields(978 responses=ConfigurableFieldMultiOption(979 id="chat_responses",980 name="Chat Responses",981 options={982 "hello": "A good morning to you!",983 "bye": "See you later!",984 "helpful": "How can I help you?",985 },986 default=["hello", "bye"],987 )988 )989 fake_llm = (990 FakeListLLM(responses=["a"])991 .configurable_fields(992 responses=ConfigurableField(993 id="llm_responses",994 name="LLM Responses",995 description="A list of fake responses for this LLM",996 )997 )998 .configurable_alternatives(999 ConfigurableField(id="llm", name="LLM"),1000 chat=fake_chat | StrOutputParser(),1001 )1002 )10031004 prompt = PromptTemplate.from_template("Hello, {name}!").configurable_fields(1005 template=ConfigurableFieldSingleOption(1006 id="prompt_template",1007 name="Prompt Template",1008 description="The prompt template for this chain",1009 options={1010 "hello": "Hello, {name}!",1011 "good_morning": "A very good morning to you, {name}!",1012 },1013 default="hello",1014 )1015 )10161017 # deduplication of configurable fields1018 chain_configurable = prompt | fake_llm | (lambda x: {"name": x}) | prompt | fake_llm10191020 assert chain_configurable.invoke({"name": "John"}) == "a"10211022 if PYDANTIC_VERSION_AT_LEAST_29:1023 assert _normalize_schema(1024 chain_configurable.get_config_jsonschema()1025 ) == snapshot(name="schema7")10261027 assert (1028 chain_configurable.with_config(configurable={"llm": "chat"}).invoke(1029 {"name": "John"}1030 )1031 == "A good morning to you!"1032 )10331034 assert (1035 chain_configurable.with_config(1036 configurable={"llm": "chat", "chat_responses": ["helpful"]}1037 ).invoke({"name": "John"})1038 == "How can I help you?"1039 )104010411042def test_passthrough_tap(mocker: MockerFixture) -> None:1043 fake = FakeRunnable()1044 mock = mocker.Mock()10451046 seq = RunnablePassthrough[Any](mock) | fake | RunnablePassthrough[Any](mock)10471048 assert seq.invoke("hello", my_kwarg="value") == 51049 assert mock.call_args_list == [1050 mocker.call("hello", my_kwarg="value"),1051 mocker.call(5),1052 ]1053 mock.reset_mock()10541055 assert seq.batch(["hello", "byebye"], my_kwarg="value") == [5, 6]1056 assert len(mock.call_args_list) == 41057 for call in [1058 mocker.call("hello", my_kwarg="value"),1059 mocker.call("byebye", my_kwarg="value"),1060 mocker.call(5),1061 mocker.call(6),1062 ]:1063 assert call in mock.call_args_list1064 mock.reset_mock()10651066 assert seq.batch(["hello", "byebye"], my_kwarg="value", return_exceptions=True) == [1067 5,1068 6,1069 ]1070 assert len(mock.call_args_list) == 41071 for call in [1072 mocker.call("hello", my_kwarg="value"),1073 mocker.call("byebye", my_kwarg="value"),1074 mocker.call(5),1075 mocker.call(6),1076 ]:1077 assert call in mock.call_args_list1078 mock.reset_mock()10791080 assert sorted(1081 a1082 for a in seq.batch_as_completed(1083 ["hello", "byebye"], my_kwarg="value", return_exceptions=True1084 )1085 ) == [1086 (0, 5),1087 (1, 6),1088 ]1089 assert len(mock.call_args_list) == 41090 for call in [1091 mocker.call("hello", my_kwarg="value"),1092 mocker.call("byebye", my_kwarg="value"),1093 mocker.call(5),1094 mocker.call(6),1095 ]:1096 assert call in mock.call_args_list1097 mock.reset_mock()10981099 assert list(1100 seq.stream("hello", {"metadata": {"key": "value"}}, my_kwarg="value")1101 ) == [5]1102 assert mock.call_args_list == [1103 mocker.call("hello", my_kwarg="value"),1104 mocker.call(5),1105 ]1106 mock.reset_mock()110711081109async def test_passthrough_tap_async(mocker: MockerFixture) -> None:1110 fake = FakeRunnable()1111 mock = mocker.Mock()11121113 seq = RunnablePassthrough[Any](mock) | fake | RunnablePassthrough[Any](mock)11141115 assert await seq.ainvoke("hello", my_kwarg="value") == 51116 assert mock.call_args_list == [1117 mocker.call("hello", my_kwarg="value"),1118 mocker.call(5),1119 ]1120 mock.reset_mock()11211122 assert await seq.abatch(["hello", "byebye"], my_kwarg="value") == [5, 6]1123 assert len(mock.call_args_list) == 41124 for call in [1125 mocker.call("hello", my_kwarg="value"),1126 mocker.call("byebye", my_kwarg="value"),1127 mocker.call(5),1128 mocker.call(6),1129 ]:1130 assert call in mock.call_args_list1131 mock.reset_mock()11321133 assert await seq.abatch(1134 ["hello", "byebye"], my_kwarg="value", return_exceptions=True1135 ) == [1136 5,1137 6,1138 ]1139 assert len(mock.call_args_list) == 41140 for call in [1141 mocker.call("hello", my_kwarg="value"),1142 mocker.call("byebye", my_kwarg="value"),1143 mocker.call(5),1144 mocker.call(6),1145 ]:1146 assert call in mock.call_args_list1147 mock.reset_mock()11481149 assert sorted(1150 [1151 a1152 async for a in seq.abatch_as_completed(1153 ["hello", "byebye"], my_kwarg="value", return_exceptions=True1154 )1155 ]1156 ) == [1157 (0, 5),1158 (1, 6),1159 ]1160 assert len(mock.call_args_list) == 41161 for call in [1162 mocker.call("hello", my_kwarg="value"),1163 mocker.call("byebye", my_kwarg="value"),1164 mocker.call(5),1165 mocker.call(6),1166 ]:1167 assert call in mock.call_args_list1168 mock.reset_mock()11691170 assert [1171 part1172 async for part in seq.astream(1173 "hello", {"metadata": {"key": "value"}}, my_kwarg="value"1174 )1175 ] == [5]1176 assert mock.call_args_list == [1177 mocker.call("hello", my_kwarg="value"),1178 mocker.call(5),1179 ]118011811182async def test_with_config_metadata_passthrough(mocker: MockerFixture) -> None:1183 fake = FakeRunnableSerializable()1184 spy = mocker.spy(fake.__class__, "invoke")1185 fakew = fake.configurable_fields(hello=ConfigurableField(id="hello", name="Hello"))11861187 assert (1188 fakew.with_config(tags=["a-tag"]).invoke(1189 "hello",1190 {1191 "configurable": {"hello": "there", "__secret_key": "nahnah"},1192 "metadata": {"bye": "now"},1193 },1194 )1195 == 51196 )1197 assert spy.call_args_list[0].args[1:] == (1198 "hello",1199 {1200 "tags": ["a-tag"],1201 "callbacks": None,1202 "recursion_limit": 25,1203 "configurable": {"hello": "there", "__secret_key": "nahnah"},1204 "metadata": {"bye": "now"},1205 },1206 )1207 spy.reset_mock()120812091210def test_with_config(mocker: MockerFixture) -> None:1211 fake = FakeRunnable()1212 spy = mocker.spy(fake, "invoke")12131214 assert fake.with_config(tags=["a-tag"]).invoke("hello") == 51215 assert spy.call_args_list == [1216 mocker.call(1217 "hello",1218 {"tags": ["a-tag"], "metadata": {}, "configurable": {}},1219 ),1220 ]1221 spy.reset_mock()12221223 fake_1 = RunnablePassthrough[Any]()1224 fake_2 = RunnablePassthrough[Any]()1225 spy_seq_step = mocker.spy(fake_1.__class__, "invoke")12261227 sequence = fake_1.with_config(tags=["a-tag"]) | fake_2.with_config(1228 tags=["b-tag"], max_concurrency=51229 )1230 assert sequence.invoke("hello") == "hello"1231 assert len(spy_seq_step.call_args_list) == 21232 for i, call in enumerate(spy_seq_step.call_args_list):1233 assert call.args[1] == "hello"1234 if i == 0:1235 assert call.args[2].get("tags") == ["a-tag"]1236 assert call.args[2].get("max_concurrency") is None1237 else:1238 assert call.args[2].get("tags") == ["b-tag"]1239 assert call.args[2].get("max_concurrency") == 51240 mocker.stop(spy_seq_step)12411242 assert [1243 *fake.with_config(tags=["a-tag"]).stream(1244 "hello", {"metadata": {"key": "value"}}1245 )1246 ] == [5]1247 assert spy.call_args_list == [1248 mocker.call(1249 "hello",1250 {"tags": ["a-tag"], "metadata": {"key": "value"}, "configurable": {}},1251 ),1252 ]1253 spy.reset_mock()12541255 assert fake.with_config(recursion_limit=5).batch(1256 ["hello", "wooorld"], [{"tags": ["a-tag"]}, {"metadata": {"key": "value"}}]1257 ) == [5, 7]12581259 assert len(spy.call_args_list) == 21260 for i, call in enumerate(1261 sorted(spy.call_args_list, key=lambda x: 0 if x.args[0] == "hello" else 1)1262 ):1263 assert call.args[0] == ("hello" if i == 0 else "wooorld")1264 if i == 0:1265 assert call.args[1].get("recursion_limit") == 51266 assert call.args[1].get("tags") == ["a-tag"]1267 assert call.args[1].get("metadata") == {}1268 else:1269 assert call.args[1].get("recursion_limit") == 51270 assert call.args[1].get("tags") == []1271 assert call.args[1].get("metadata") == {"key": "value"}12721273 spy.reset_mock()12741275 assert sorted(1276 c1277 for c in fake.with_config(recursion_limit=5).batch_as_completed(1278 ["hello", "wooorld"],1279 [{"tags": ["a-tag"]}, {"metadata": {"key": "value"}}],1280 )1281 ) == [(0, 5), (1, 7)]12821283 assert len(spy.call_args_list) == 21284 for i, call in enumerate(1285 sorted(spy.call_args_list, key=lambda x: 0 if x.args[0] == "hello" else 1)1286 ):1287 assert call.args[0] == ("hello" if i == 0 else "wooorld")1288 if i == 0:1289 assert call.args[1].get("recursion_limit") == 51290 assert call.args[1].get("tags") == ["a-tag"]1291 assert call.args[1].get("metadata") == {}1292 else:1293 assert call.args[1].get("recursion_limit") == 51294 assert call.args[1].get("tags") == []1295 assert call.args[1].get("metadata") == {"key": "value"}12961297 spy.reset_mock()12981299 assert fake.with_config(metadata={"a": "b"}).batch(1300 ["hello", "wooorld"], {"tags": ["a-tag"]}1301 ) == [5, 7]1302 assert len(spy.call_args_list) == 21303 for i, call in enumerate(spy.call_args_list):1304 assert call.args[0] == ("hello" if i == 0 else "wooorld")1305 assert call.args[1].get("tags") == ["a-tag"]1306 assert call.args[1].get("metadata") == {"a": "b"}1307 spy.reset_mock()13081309 assert sorted(1310 c for c in fake.batch_as_completed(["hello", "wooorld"], {"tags": ["a-tag"]})1311 ) == [(0, 5), (1, 7)]1312 assert len(spy.call_args_list) == 21313 for i, call in enumerate(spy.call_args_list):1314 assert call.args[0] == ("hello" if i == 0 else "wooorld")1315 assert call.args[1].get("tags") == ["a-tag"]131613171318async def test_with_config_async(mocker: MockerFixture) -> None:1319 fake = FakeRunnable()1320 spy = mocker.spy(fake, "invoke")13211322 handler = ConsoleCallbackHandler()1323 assert (1324 await fake.with_config(metadata={"a": "b"}).ainvoke(1325 "hello", config={"callbacks": [handler]}1326 )1327 == 51328 )1329 assert spy.call_args_list == [1330 mocker.call(1331 "hello",1332 {1333 "callbacks": [handler],1334 "metadata": {"a": "b"},1335 "configurable": {},1336 "tags": [],1337 },1338 ),1339 ]1340 spy.reset_mock()13411342 assert [1343 part async for part in fake.with_config(metadata={"a": "b"}).astream("hello")1344 ] == [5]1345 assert spy.call_args_list == [1346 mocker.call("hello", {"metadata": {"a": "b"}, "tags": [], "configurable": {}}),1347 ]1348 spy.reset_mock()13491350 assert await fake.with_config(recursion_limit=5, tags=["c"]).abatch(1351 ["hello", "wooorld"], {"metadata": {"key": "value"}}1352 ) == [1353 5,1354 7,1355 ]1356 assert sorted(spy.call_args_list) == [1357 mocker.call(1358 "hello",1359 {1360 "metadata": {"key": "value"},1361 "tags": ["c"],1362 "callbacks": None,1363 "recursion_limit": 5,1364 "configurable": {},1365 },1366 ),1367 mocker.call(1368 "wooorld",1369 {1370 "metadata": {"key": "value"},1371 "tags": ["c"],1372 "callbacks": None,1373 "recursion_limit": 5,1374 "configurable": {},1375 },1376 ),1377 ]1378 spy.reset_mock()13791380 assert sorted(1381 [1382 c1383 async for c in fake.with_config(1384 recursion_limit=5, tags=["c"]1385 ).abatch_as_completed(["hello", "wooorld"], {"metadata": {"key": "value"}})1386 ]1387 ) == [1388 (0, 5),1389 (1, 7),1390 ]1391 assert len(spy.call_args_list) == 21392 first_call = next(call for call in spy.call_args_list if call.args[0] == "hello")1393 assert first_call == mocker.call(1394 "hello",1395 {1396 "metadata": {"key": "value"},1397 "tags": ["c"],1398 "callbacks": None,1399 "recursion_limit": 5,1400 "configurable": {},1401 },1402 )1403 second_call = next(call for call in spy.call_args_list if call.args[0] == "wooorld")1404 assert second_call == mocker.call(1405 "wooorld",1406 {1407 "metadata": {"key": "value"},1408 "tags": ["c"],1409 "callbacks": None,1410 "recursion_limit": 5,1411 "configurable": {},1412 },1413 )141414151416def test_default_method_implementations(mocker: MockerFixture) -> None:1417 fake = FakeRunnable()1418 spy = mocker.spy(fake, "invoke")14191420 assert fake.invoke("hello", {"tags": ["a-tag"]}) == 51421 assert spy.call_args_list == [1422 mocker.call("hello", {"tags": ["a-tag"]}),1423 ]1424 spy.reset_mock()14251426 assert [*fake.stream("hello", {"metadata": {"key": "value"}})] == [5]1427 assert spy.call_args_list == [1428 mocker.call("hello", {"metadata": {"key": "value"}}),1429 ]1430 spy.reset_mock()14311432 assert fake.batch(1433 ["hello", "wooorld"], [{"tags": ["a-tag"]}, {"metadata": {"key": "value"}}]1434 ) == [5, 7]14351436 assert len(spy.call_args_list) == 21437 for call in spy.call_args_list:1438 call_arg = call.args[0]14391440 if call_arg == "hello":1441 assert call_arg == "hello"1442 assert call.args[1].get("tags") == ["a-tag"]1443 assert call.args[1].get("metadata") == {}1444 else:1445 assert call_arg == "wooorld"1446 assert call.args[1].get("tags") == []1447 assert call.args[1].get("metadata") == {"key": "value"}14481449 spy.reset_mock()14501451 assert fake.batch(["hello", "wooorld"], {"tags": ["a-tag"]}) == [5, 7]1452 assert len(spy.call_args_list) == 21453 assert {call.args[0] for call in spy.call_args_list} == {"hello", "wooorld"}1454 for call in spy.call_args_list:1455 assert call.args[1].get("tags") == ["a-tag"]1456 assert call.args[1].get("metadata") == {}145714581459async def test_default_method_implementations_async(mocker: MockerFixture) -> None:1460 fake = FakeRunnable()1461 spy = mocker.spy(fake, "invoke")14621463 assert await fake.ainvoke("hello", config={"callbacks": []}) == 51464 assert spy.call_args_list == [1465 mocker.call("hello", {"callbacks": []}),1466 ]1467 spy.reset_mock()14681469 assert [part async for part in fake.astream("hello")] == [5]1470 assert spy.call_args_list == [1471 mocker.call("hello", None),1472 ]1473 spy.reset_mock()14741475 assert await fake.abatch(["hello", "wooorld"], {"metadata": {"key": "value"}}) == [1476 5,1477 7,1478 ]1479 assert {call.args[0] for call in spy.call_args_list} == {"hello", "wooorld"}1480 for call in spy.call_args_list:1481 assert call.args[1] == {1482 "metadata": {"key": "value"},1483 "tags": [],1484 "callbacks": None,1485 "recursion_limit": 25,1486 "configurable": {},1487 }148814891490def test_prompt() -> None:1491 prompt = ChatPromptTemplate.from_messages(1492 messages=[1493 SystemMessage(content="You are a nice assistant."),1494 HumanMessagePromptTemplate.from_template("{question}"),1495 ]1496 )1497 expected = ChatPromptValue(1498 messages=[1499 SystemMessage(content="You are a nice assistant."),1500 HumanMessage(content="What is your name?"),1501 ]1502 )15031504 assert prompt.invoke({"question": "What is your name?"}) == expected15051506 assert prompt.batch(1507 [1508 {"question": "What is your name?"},1509 {"question": "What is your favorite color?"},1510 ]1511 ) == [1512 expected,1513 ChatPromptValue(1514 messages=[1515 SystemMessage(content="You are a nice assistant."),1516 HumanMessage(content="What is your favorite color?"),1517 ]1518 ),1519 ]15201521 assert [*prompt.stream({"question": "What is your name?"})] == [expected]152215231524async def test_prompt_async() -> None:1525 prompt = ChatPromptTemplate.from_messages(1526 messages=[1527 SystemMessage(content="You are a nice assistant."),1528 HumanMessagePromptTemplate.from_template("{question}"),1529 ]1530 )1531 expected = ChatPromptValue(1532 messages=[1533 SystemMessage(content="You are a nice assistant."),1534 HumanMessage(content="What is your name?"),1535 ]1536 )15371538 assert await prompt.ainvoke({"question": "What is your name?"}) == expected15391540 assert await prompt.abatch(1541 [1542 {"question": "What is your name?"},1543 {"question": "What is your favorite color?"},1544 ]1545 ) == [1546 expected,1547 ChatPromptValue(1548 messages=[1549 SystemMessage(content="You are a nice assistant."),1550 HumanMessage(content="What is your favorite color?"),1551 ]1552 ),1553 ]15541555 assert [1556 part async for part in prompt.astream({"question": "What is your name?"})1557 ] == [expected]15581559 stream_log = [1560 part async for part in prompt.astream_log({"question": "What is your name?"})1561 ]15621563 assert len(stream_log[0].ops) == 11564 assert stream_log[0].ops[0]["op"] == "replace"1565 assert stream_log[0].ops[0]["path"] == ""1566 assert stream_log[0].ops[0]["value"]["logs"] == {}1567 assert stream_log[0].ops[0]["value"]["final_output"] is None1568 assert stream_log[0].ops[0]["value"]["streamed_output"] == []1569 assert isinstance(stream_log[0].ops[0]["value"]["id"], str)15701571 assert stream_log[1:] == [1572 RunLogPatch(1573 {"op": "add", "path": "/streamed_output/-", "value": expected},1574 {1575 "op": "replace",1576 "path": "/final_output",1577 "value": ChatPromptValue(1578 messages=[1579 SystemMessage(content="You are a nice assistant."),1580 HumanMessage(content="What is your name?"),1581 ]1582 ),1583 },1584 ),1585 ]15861587 stream_log_state = [1588 part1589 async for part in prompt.astream_log(1590 {"question": "What is your name?"}, diff=False1591 )1592 ]15931594 # remove random id1595 stream_log[0].ops[0]["value"]["id"] = "00000000-0000-0000-0000-000000000000"1596 stream_log_state[-1].ops[0]["value"]["id"] = "00000000-0000-0000-0000-000000000000"1597 stream_log_state[-1].state["id"] = "00000000-0000-0000-0000-000000000000"15981599 # assert output with diff=False matches output with diff=True1600 assert stream_log_state[-1].ops == [op for chunk in stream_log for op in chunk.ops]1601 assert stream_log_state[-1] == RunLog(1602 *[op for chunk in stream_log for op in chunk.ops],1603 state={1604 "final_output": ChatPromptValue(1605 messages=[1606 SystemMessage(content="You are a nice assistant."),1607 HumanMessage(content="What is your name?"),1608 ]1609 ),1610 "id": "00000000-0000-0000-0000-000000000000",1611 "logs": {},1612 "streamed_output": [1613 ChatPromptValue(1614 messages=[1615 SystemMessage(content="You are a nice assistant."),1616 HumanMessage(content="What is your name?"),1617 ]1618 )1619 ],1620 "type": "prompt",1621 "name": "ChatPromptTemplate",1622 },1623 )16241625 # nested inside trace_with_chain_group16261627 async with atrace_as_chain_group("a_group") as manager:1628 stream_log_nested = [1629 part1630 async for part in prompt.astream_log(1631 {"question": "What is your name?"}, config={"callbacks": manager}1632 )1633 ]16341635 assert len(stream_log_nested[0].ops) == 11636 assert stream_log_nested[0].ops[0]["op"] == "replace"1637 assert stream_log_nested[0].ops[0]["path"] == ""1638 assert stream_log_nested[0].ops[0]["value"]["logs"] == {}1639 assert stream_log_nested[0].ops[0]["value"]["final_output"] is None1640 assert stream_log_nested[0].ops[0]["value"]["streamed_output"] == []1641 assert isinstance(stream_log_nested[0].ops[0]["value"]["id"], str)16421643 assert stream_log_nested[1:] == [1644 RunLogPatch(1645 {"op": "add", "path": "/streamed_output/-", "value": expected},1646 {1647 "op": "replace",1648 "path": "/final_output",1649 "value": ChatPromptValue(1650 messages=[1651 SystemMessage(content="You are a nice assistant."),1652 HumanMessage(content="What is your name?"),1653 ]1654 ),1655 },1656 ),1657 ]165816591660def test_prompt_template_params() -> None:1661 prompt = ChatPromptTemplate.from_template(1662 "Respond to the following question: {question}"1663 )1664 result = prompt.invoke(1665 {1666 "question": "test",1667 "topic": "test",1668 }1669 )1670 assert result == ChatPromptValue(1671 messages=[HumanMessage(content="Respond to the following question: test")]1672 )16731674 with pytest.raises(KeyError):1675 prompt.invoke({})167616771678def test_with_listeners(mocker: MockerFixture) -> None:1679 prompt = (1680 SystemMessagePromptTemplate.from_template("You are a nice assistant.")1681 + "{question}"1682 )1683 chat = FakeListChatModel(responses=["foo"])16841685 chain = prompt | chat16861687 mock_start = mocker.Mock()1688 mock_end = mocker.Mock()16891690 chain.with_listeners(on_start=mock_start, on_end=mock_end).invoke(1691 {"question": "Who are you?"}1692 )16931694 assert mock_start.call_count == 11695 assert mock_start.call_args[0][0].name == "RunnableSequence"1696 assert mock_end.call_count == 116971698 mock_start.reset_mock()1699 mock_end.reset_mock()17001701 with trace_as_chain_group("hello") as manager:1702 chain.with_listeners(on_start=mock_start, on_end=mock_end).invoke(1703 {"question": "Who are you?"}, {"callbacks": manager}1704 )17051706 assert mock_start.call_count == 11707 assert mock_start.call_args[0][0].name == "RunnableSequence"1708 assert mock_end.call_count == 1170917101711async def test_with_listeners_async(mocker: MockerFixture) -> None:1712 prompt = (1713 SystemMessagePromptTemplate.from_template("You are a nice assistant.")1714 + "{question}"1715 )1716 chat = FakeListChatModel(responses=["foo"])17171718 chain = prompt | chat17191720 mock_start = mocker.Mock()1721 mock_end = mocker.Mock()17221723 await chain.with_listeners(on_start=mock_start, on_end=mock_end).ainvoke(1724 {"question": "Who are you?"}1725 )17261727 assert mock_start.call_count == 11728 assert mock_start.call_args[0][0].name == "RunnableSequence"1729 assert mock_end.call_count == 117301731 mock_start.reset_mock()1732 mock_end.reset_mock()17331734 async with atrace_as_chain_group("hello") as manager:1735 await chain.with_listeners(on_start=mock_start, on_end=mock_end).ainvoke(1736 {"question": "Who are you?"}, {"callbacks": manager}1737 )17381739 assert mock_start.call_count == 11740 assert mock_start.call_args[0][0].name == "RunnableSequence"1741 assert mock_end.call_count == 1174217431744def test_with_listener_propagation(mocker: MockerFixture) -> None:1745 prompt = (1746 SystemMessagePromptTemplate.from_template("You are a nice assistant.")1747 + "{question}"1748 )1749 chat = FakeListChatModel(responses=["foo"])1750 chain = prompt | chat1751 mock_start = mocker.Mock()1752 mock_end = mocker.Mock()1753 chain_with_listeners = chain.with_listeners(on_start=mock_start, on_end=mock_end)17541755 chain_with_listeners.with_retry().invoke({"question": "Who are you?"})17561757 assert mock_start.call_count == 11758 assert mock_start.call_args[0][0].name == "RunnableSequence"1759 assert mock_end.call_count == 117601761 mock_start.reset_mock()1762 mock_end.reset_mock()17631764 chain_with_listeners.invoke({"question": "Who are you?"})17651766 assert mock_start.call_count == 11767 assert mock_start.call_args[0][0].name == "RunnableSequence"1768 assert mock_end.call_count == 117691770 mock_start.reset_mock()1771 mock_end.reset_mock()17721773 chain_with_listeners.with_config({"tags": ["foo"]}).invoke(1774 {"question": "Who are you?"}1775 )17761777 assert mock_start.call_count == 11778 assert mock_start.call_args[0][0].name == "RunnableSequence"1779 assert mock_end.call_count == 117801781 mock_start.reset_mock()1782 mock_end.reset_mock()17831784 chain_with_listeners.bind(stop=["foo"]).invoke({"question": "Who are you?"})17851786 assert mock_start.call_count == 11787 assert mock_start.call_args[0][0].name == "RunnableSequence"1788 assert mock_end.call_count == 117891790 mock_start.reset_mock()1791 mock_end.reset_mock()17921793 mock_start_inner = mocker.Mock()1794 mock_end_inner = mocker.Mock()17951796 chain_with_listeners.with_listeners(1797 on_start=mock_start_inner, on_end=mock_end_inner1798 ).invoke({"question": "Who are you?"})17991800 assert mock_start.call_count == 11801 assert mock_start.call_args[0][0].name == "RunnableSequence"1802 assert mock_end.call_count == 11803 assert mock_start_inner.call_count == 11804 assert mock_start_inner.call_args[0][0].name == "RunnableSequence"1805 assert mock_end_inner.call_count == 1180618071808@freeze_time("2023-01-01")1809@pytest.mark.usefixtures("deterministic_uuids")1810def test_prompt_with_chat_model(1811 mocker: MockerFixture,1812 snapshot: SnapshotAssertion,1813) -> None:1814 prompt = (1815 SystemMessagePromptTemplate.from_template("You are a nice assistant.")1816 + "{question}"1817 )1818 chat = FakeListChatModel(responses=["foo"])18191820 chain = prompt | chat18211822 assert _normalize_lc_version(repr(chain)) == snapshot1823 assert isinstance(chain, RunnableSequence)1824 assert chain.first == prompt1825 assert chain.middle == []1826 assert chain.last == chat1827 assert _normalize_lc_version(dumps(chain, pretty=True)) == snapshot18281829 # Test invoke1830 prompt_spy = mocker.spy(prompt.__class__, "invoke")1831 chat_spy = mocker.spy(chat.__class__, "invoke")1832 tracer = FakeTracer()1833 assert chain.invoke(1834 {"question": "What is your name?"}, {"callbacks": [tracer]}1835 ) == _any_id_ai_message(content="foo")1836 assert prompt_spy.call_args.args[1] == {"question": "What is your name?"}1837 assert chat_spy.call_args.args[1] == ChatPromptValue(1838 messages=[1839 SystemMessage(content="You are a nice assistant."),1840 HumanMessage(content="What is your name?"),1841 ]1842 )18431844 assert tracer.runs == snapshot18451846 mocker.stop(prompt_spy)1847 mocker.stop(chat_spy)18481849 # Test batch1850 prompt_spy = mocker.spy(prompt.__class__, "batch")1851 chat_spy = mocker.spy(chat.__class__, "batch")1852 tracer = FakeTracer()1853 assert chain.batch(1854 [1855 {"question": "What is your name?"},1856 {"question": "What is your favorite color?"},1857 ],1858 {"callbacks": [tracer]},1859 ) == [1860 _any_id_ai_message(content="foo"),1861 _any_id_ai_message(content="foo"),1862 ]1863 assert prompt_spy.call_args.args[1] == [1864 {"question": "What is your name?"},1865 {"question": "What is your favorite color?"},1866 ]1867 assert chat_spy.call_args.args[1] == [1868 ChatPromptValue(1869 messages=[1870 SystemMessage(content="You are a nice assistant."),1871 HumanMessage(content="What is your name?"),1872 ]1873 ),1874 ChatPromptValue(1875 messages=[1876 SystemMessage(content="You are a nice assistant."),1877 HumanMessage(content="What is your favorite color?"),1878 ]1879 ),1880 ]1881 assert (1882 len(1883 [1884 r1885 for r in tracer.runs1886 if r.parent_run_id is None and len(r.child_runs) == 21887 ]1888 )1889 == 21890 ), "Each of 2 outer runs contains exactly two inner runs (1 prompt, 1 chat)"1891 mocker.stop(prompt_spy)1892 mocker.stop(chat_spy)18931894 # Test stream1895 prompt_spy = mocker.spy(prompt.__class__, "invoke")1896 chat_spy = mocker.spy(chat.__class__, "stream")1897 tracer = FakeTracer()1898 assert [1899 *chain.stream({"question": "What is your name?"}, {"callbacks": [tracer]})1900 ] == [1901 _any_id_ai_message_chunk(content="f"),1902 _any_id_ai_message_chunk(content="o"),1903 _any_id_ai_message_chunk(content="o", chunk_position="last"),1904 ]1905 assert prompt_spy.call_args.args[1] == {"question": "What is your name?"}1906 assert chat_spy.call_args.args[1] == ChatPromptValue(1907 messages=[1908 SystemMessage(content="You are a nice assistant."),1909 HumanMessage(content="What is your name?"),1910 ]1911 )191219131914@freeze_time("2023-01-01")1915@pytest.mark.usefixtures("deterministic_uuids")1916async def test_prompt_with_chat_model_async(1917 mocker: MockerFixture,1918 snapshot: SnapshotAssertion,1919) -> None:1920 prompt = (1921 SystemMessagePromptTemplate.from_template("You are a nice assistant.")1922 + "{question}"1923 )1924 chat = FakeListChatModel(responses=["foo"])19251926 chain = prompt | chat19271928 assert _normalize_lc_version(repr(chain)) == snapshot1929 assert isinstance(chain, RunnableSequence)1930 assert chain.first == prompt1931 assert chain.middle == []1932 assert chain.last == chat1933 assert _normalize_lc_version(dumps(chain, pretty=True)) == snapshot19341935 # Test invoke1936 prompt_spy = mocker.spy(prompt.__class__, "ainvoke")1937 chat_spy = mocker.spy(chat.__class__, "ainvoke")1938 tracer = FakeTracer()1939 assert await chain.ainvoke(1940 {"question": "What is your name?"}, {"callbacks": [tracer]}1941 ) == _any_id_ai_message(content="foo")1942 assert prompt_spy.call_args.args[1] == {"question": "What is your name?"}1943 assert chat_spy.call_args.args[1] == ChatPromptValue(1944 messages=[1945 SystemMessage(content="You are a nice assistant."),1946 HumanMessage(content="What is your name?"),1947 ]1948 )19491950 assert tracer.runs == snapshot19511952 mocker.stop(prompt_spy)1953 mocker.stop(chat_spy)19541955 # Test batch1956 prompt_spy = mocker.spy(prompt.__class__, "abatch")1957 chat_spy = mocker.spy(chat.__class__, "abatch")1958 tracer = FakeTracer()1959 assert await chain.abatch(1960 [1961 {"question": "What is your name?"},1962 {"question": "What is your favorite color?"},1963 ],1964 {"callbacks": [tracer]},1965 ) == [1966 _any_id_ai_message(content="foo"),1967 _any_id_ai_message(content="foo"),1968 ]1969 assert prompt_spy.call_args.args[1] == [1970 {"question": "What is your name?"},1971 {"question": "What is your favorite color?"},1972 ]1973 assert chat_spy.call_args.args[1] == [1974 ChatPromptValue(1975 messages=[1976 SystemMessage(content="You are a nice assistant."),1977 HumanMessage(content="What is your name?"),1978 ]1979 ),1980 ChatPromptValue(1981 messages=[1982 SystemMessage(content="You are a nice assistant."),1983 HumanMessage(content="What is your favorite color?"),1984 ]1985 ),1986 ]1987 assert (1988 len(1989 [1990 r1991 for r in tracer.runs1992 if r.parent_run_id is None and len(r.child_runs) == 21993 ]1994 )1995 == 21996 ), "Each of 2 outer runs contains exactly two inner runs (1 prompt, 1 chat)"1997 mocker.stop(prompt_spy)1998 mocker.stop(chat_spy)19992000 # Test stream
Findings
✓ No findings reported for this file.