core[patch]: Improve some error messages and add another test for checking RunnableWithMessageHistory (#25209)

Also add more useful error messages.

---------

Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
This commit is contained in:
William FH 2024-08-22 11:14:27 -07:00 committed by GitHub
parent b4fcda7657
commit 8230ba47f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 27 additions and 5 deletions

View File

@ -214,7 +214,7 @@ def deprecated(
if not _obj_type:
_obj_type = "attribute"
if not _name:
raise ValueError()
raise ValueError(f"Field {obj} must have a name to be deprecated.")
old_doc = obj.description
def finalize(wrapper: Callable[..., Any], new_doc: str) -> T:

View File

@ -551,13 +551,13 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate):
input_variables=input_variables, template=img_template
)
else:
raise ValueError()
raise ValueError(f"Invalid image template: {tmpl}")
prompt.append(img_template_obj)
else:
raise ValueError()
raise ValueError(f"Invalid template: {tmpl}")
return cls(prompt=prompt, **kwargs)
else:
raise ValueError()
raise ValueError(f"Invalid template: {template}")
@classmethod
def from_template_file(

View File

@ -433,7 +433,9 @@ class RunnableWithMessageHistory(RunnableBindingBase):
# This occurs for chat models - since we batch inputs
if isinstance(input_val[0], list):
if len(input_val) != 1:
raise ValueError()
raise ValueError(
f"Expected a single list of messages. Got {input_val}."
)
return input_val[0]
return list(input_val)
else:

View File

@ -1889,6 +1889,25 @@ async def test_runnable_with_message_history() -> None:
input_messages_key="question",
history_messages_key="history",
)
# patch with_message_history._get_output_messages to listen for errors
# so we can raise them in this main thread
raised_errors = []
def collect_errors(fn): # type: ignore
nonlocal raised_errors
def _get_output_messages(*args, **kwargs): # type: ignore
try:
return fn(*args, **kwargs)
except Exception as e:
raised_errors.append(e)
raise e
return _get_output_messages
old_ref = with_message_history._get_output_messages
with_message_history.__dict__["_get_output_messages"] = collect_errors(old_ref)
await with_message_history.with_config(
{"configurable": {"session_id": "session-123"}}
).ainvoke({"question": "hello"})
@ -1911,6 +1930,7 @@ async def test_runnable_with_message_history() -> None:
AIMessage(content="world", id="ai4"),
]
}
assert not raised_errors
EXPECTED_EVENTS = [