mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-21 14:18:52 +00:00
Update Prompt Format Error (#14044)
The number of times I try to format a string (especially in lcel) is embarrassingly high. Think this may be more actionable than the default error message. Now I get nice helpful errors ``` KeyError: "Input to ChatPromptTemplate is missing variable 'input'. Expected: ['input'] Received: ['dialogue']" ```
This commit is contained in:
parent
71c2e184b4
commit
528fc76d6a
@ -67,13 +67,27 @@ class BasePromptTemplate(RunnableSerializable[Dict, PromptValue], ABC):
|
||||
**{k: (self.input_types.get(k, str), None) for k in self.input_variables},
|
||||
)
|
||||
|
||||
def _format_prompt_with_error_handling(self, inner_input: Dict) -> PromptValue:
|
||||
try:
|
||||
input_dict = {key: inner_input[key] for key in self.input_variables}
|
||||
except TypeError as e:
|
||||
raise TypeError(
|
||||
f"Expected mapping type as input to {self.__class__.__name__}. "
|
||||
f"Received {type(inner_input)}."
|
||||
) from e
|
||||
except KeyError as e:
|
||||
raise KeyError(
|
||||
f"Input to {self.__class__.__name__} is missing variable {e}. "
|
||||
f" Expected: {self.input_variables}"
|
||||
f" Received: {list(inner_input.keys())}"
|
||||
) from e
|
||||
return self.format_prompt(**input_dict)
|
||||
|
||||
def invoke(
|
||||
self, input: Dict, config: Optional[RunnableConfig] = None
|
||||
) -> PromptValue:
|
||||
return self._call_with_config(
|
||||
lambda inner_input: self.format_prompt(
|
||||
**{key: inner_input[key] for key in self.input_variables}
|
||||
),
|
||||
self._format_prompt_with_error_handling,
|
||||
input,
|
||||
config,
|
||||
run_type="prompt",
|
||||
|
Loading…
Reference in New Issue
Block a user