-
Notifications
You must be signed in to change notification settings - Fork 429
Description
Description
There is no way to add metadata to the runnable object created from pydantic.
Currently, when we try to create a guard object from a 'for_pydantic' function, it internally calls GuardRunnable, and inside _validate function of this object, we are not taking metadata as input. I believe it should be implemented similarly to the ValidatorRunnable class inside the validator_runnable.py.
OR
We should add the with_metadata function inside the Guard object. Just like Validator inside ValidatorRunnable.
If we have this feature, it will be very easy for Data Scientists like me to use this guard runnable object on my existing projects, which are built on top of Langchain, giving me the ability to customize and integrate it anywhere I want with Langchain.
Why is this needed
It is needed because when creating a guard using pydantic and creating a runnable object from that guard, there is no way to pass metadata to the guard's runnable object instance. So, if the validator has metadata, it is impossible to pass it to the Guardrail runnable.
There are various methods to call a guard using the guard object. And generally, we pass metadata and prompt parameters inside those functions. But PLEASE NOTE that when we are creating a guard runnable object, it will be called via the invoke method, invoke is created by langchain, so they won't have any parameters called metadata. So it will be impossible to pass metadata for the validator if we invoke the guard runnable object.
You have taken care of it while creating ValidatorRunable, but not while creating GuardrailRunnable. I believe that while migrating, you miss this thing.
Implementation details
Simply adding the with_metadata function to the Guard will solve all the problems.
def with_metadata(self, metadata: Dict[str, Any]):
"""Assigns metadata to this validator to use during validation."""
self.metadata = metadata
return self
End result
New implementation will be like this -
from guardrails import guard
guard = Guard.from_pydantic(Pydantic_Object)
guard.with_metadata(my_metadata)
chain = prompt_template | model | guard.to_runnable()
prompt_template and model created via langchain.
chain.invoke(my_prompt_parameters)