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