mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-30 08:12:53 +00:00
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:
parent
b4fcda7657
commit
8230ba47f3
@ -214,7 +214,7 @@ def deprecated(
|
|||||||
if not _obj_type:
|
if not _obj_type:
|
||||||
_obj_type = "attribute"
|
_obj_type = "attribute"
|
||||||
if not _name:
|
if not _name:
|
||||||
raise ValueError()
|
raise ValueError(f"Field {obj} must have a name to be deprecated.")
|
||||||
old_doc = obj.description
|
old_doc = obj.description
|
||||||
|
|
||||||
def finalize(wrapper: Callable[..., Any], new_doc: str) -> T:
|
def finalize(wrapper: Callable[..., Any], new_doc: str) -> T:
|
||||||
|
@ -551,13 +551,13 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate):
|
|||||||
input_variables=input_variables, template=img_template
|
input_variables=input_variables, template=img_template
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise ValueError()
|
raise ValueError(f"Invalid image template: {tmpl}")
|
||||||
prompt.append(img_template_obj)
|
prompt.append(img_template_obj)
|
||||||
else:
|
else:
|
||||||
raise ValueError()
|
raise ValueError(f"Invalid template: {tmpl}")
|
||||||
return cls(prompt=prompt, **kwargs)
|
return cls(prompt=prompt, **kwargs)
|
||||||
else:
|
else:
|
||||||
raise ValueError()
|
raise ValueError(f"Invalid template: {template}")
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_template_file(
|
def from_template_file(
|
||||||
|
@ -433,7 +433,9 @@ class RunnableWithMessageHistory(RunnableBindingBase):
|
|||||||
# This occurs for chat models - since we batch inputs
|
# This occurs for chat models - since we batch inputs
|
||||||
if isinstance(input_val[0], list):
|
if isinstance(input_val[0], list):
|
||||||
if len(input_val) != 1:
|
if len(input_val) != 1:
|
||||||
raise ValueError()
|
raise ValueError(
|
||||||
|
f"Expected a single list of messages. Got {input_val}."
|
||||||
|
)
|
||||||
return input_val[0]
|
return input_val[0]
|
||||||
return list(input_val)
|
return list(input_val)
|
||||||
else:
|
else:
|
||||||
|
@ -1889,6 +1889,25 @@ async def test_runnable_with_message_history() -> None:
|
|||||||
input_messages_key="question",
|
input_messages_key="question",
|
||||||
history_messages_key="history",
|
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(
|
await with_message_history.with_config(
|
||||||
{"configurable": {"session_id": "session-123"}}
|
{"configurable": {"session_id": "session-123"}}
|
||||||
).ainvoke({"question": "hello"})
|
).ainvoke({"question": "hello"})
|
||||||
@ -1911,6 +1930,7 @@ async def test_runnable_with_message_history() -> None:
|
|||||||
AIMessage(content="world", id="ai4"),
|
AIMessage(content="world", id="ai4"),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
assert not raised_errors
|
||||||
|
|
||||||
|
|
||||||
EXPECTED_EVENTS = [
|
EXPECTED_EVENTS = [
|
||||||
|
Loading…
Reference in New Issue
Block a user