validators_api.md
1 --- 2 title: "Validators" 3 id: validators-api 4 description: "Validators validate LLM outputs" 5 slug: "/validators-api" 6 --- 7 8 <a id="json_schema"></a> 9 10 ## Module json\_schema 11 12 <a id="json_schema.is_valid_json"></a> 13 14 #### is\_valid\_json 15 16 ```python 17 def is_valid_json(s: str) -> bool 18 ``` 19 20 Check if the provided string is a valid JSON. 21 22 **Arguments**: 23 24 - `s`: The string to be checked. 25 26 **Returns**: 27 28 `True` if the string is a valid JSON; otherwise, `False`. 29 30 <a id="json_schema.JsonSchemaValidator"></a> 31 32 ### JsonSchemaValidator 33 34 Validates JSON content of `ChatMessage` against a specified [JSON Schema](https://json-schema.org/). 35 36 If JSON content of a message conforms to the provided schema, the message is passed along the "validated" output. 37 If the JSON content does not conform to the schema, the message is passed along the "validation_error" output. 38 In the latter case, the error message is constructed using the provided `error_template` or a default template. 39 These error ChatMessages can be used by LLMs in Haystack 2.x recovery loops. 40 41 Usage example: 42 43 ```python 44 from haystack import Pipeline 45 from haystack.components.generators.chat import OpenAIChatGenerator 46 from haystack.components.joiners import BranchJoiner 47 from haystack.components.validators import JsonSchemaValidator 48 from haystack import component 49 from haystack.dataclasses import ChatMessage 50 51 52 @component 53 class MessageProducer: 54 55 @component.output_types(messages=list[ChatMessage]) 56 def run(self, messages: list[ChatMessage]) -> dict: 57 return {"messages": messages} 58 59 60 p = Pipeline() 61 p.add_component("llm", OpenAIChatGenerator(model="gpt-4-1106-preview", 62 generation_kwargs={"response_format": {"type": "json_object"}})) 63 p.add_component("schema_validator", JsonSchemaValidator()) 64 p.add_component("joiner_for_llm", BranchJoiner(list[ChatMessage])) 65 p.add_component("message_producer", MessageProducer()) 66 67 p.connect("message_producer.messages", "joiner_for_llm") 68 p.connect("joiner_for_llm", "llm") 69 p.connect("llm.replies", "schema_validator.messages") 70 p.connect("schema_validator.validation_error", "joiner_for_llm") 71 72 result = p.run(data={ 73 "message_producer": { 74 "messages":[ChatMessage.from_user("Generate JSON for person with name 'John' and age 30")]}, 75 "schema_validator": { 76 "json_schema": { 77 "type": "object", 78 "properties": {"name": {"type": "string"}, 79 "age": {"type": "integer"} 80 } 81 } 82 } 83 }) 84 print(result) 85 >> {'schema_validator': {'validated': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, 86 _content=[TextContent(text="\n{\n "name": "John",\n "age": 30\n}")], 87 _name=None, _meta={'model': 'gpt-4-1106-preview', 'index': 0, 88 'finish_reason': 'stop', 'usage': {'completion_tokens': 17, 'prompt_tokens': 20, 'total_tokens': 37}})]}} 89 ``` 90 91 <a id="json_schema.JsonSchemaValidator.__init__"></a> 92 93 #### JsonSchemaValidator.\_\_init\_\_ 94 95 ```python 96 def __init__(json_schema: dict[str, Any] | None = None, 97 error_template: str | None = None) 98 ``` 99 100 Initialize the JsonSchemaValidator component. 101 102 **Arguments**: 103 104 - `json_schema`: A dictionary representing the [JSON schema](https://json-schema.org/) against which 105 the messages' content is validated. 106 - `error_template`: A custom template string for formatting the error message in case of validation failure. 107 108 <a id="json_schema.JsonSchemaValidator.run"></a> 109 110 #### JsonSchemaValidator.run 111 112 ```python 113 @component.output_types(validated=list[ChatMessage], 114 validation_error=list[ChatMessage]) 115 def run(messages: list[ChatMessage], 116 json_schema: dict[str, Any] | None = None, 117 error_template: str | None = None) -> dict[str, list[ChatMessage]] 118 ``` 119 120 Validates the last of the provided messages against the specified json schema. 121 122 If it does, the message is passed along the "validated" output. If it does not, the message is passed along 123 the "validation_error" output. 124 125 **Arguments**: 126 127 - `messages`: A list of ChatMessage instances to be validated. The last message in this list is the one 128 that is validated. 129 - `json_schema`: A dictionary representing the [JSON schema](https://json-schema.org/) 130 against which the messages' content is validated. If not provided, the schema from the component init 131 is used. 132 - `error_template`: A custom template string for formatting the error message in case of validation. If not 133 provided, the `error_template` from the component init is used. 134 135 **Raises**: 136 137 - `ValueError`: If no JSON schema is provided or if the message content is not a dictionary or a list of 138 dictionaries. 139 140 **Returns**: 141 142 A dictionary with the following keys: 143 - "validated": A list of messages if the last message is valid. 144 - "validation_error": A list of messages if the last message is invalid. 145