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(model="gpt-4-1106-preview",
 57                                             generation_kwargs={"response_format": {"type": "json_object"}}))
 58  p.add_component("schema_validator", JsonSchemaValidator())
 59  p.add_component("joiner_for_llm", BranchJoiner(list[ChatMessage]))
 60  p.add_component("message_producer", MessageProducer())
 61  
 62  p.connect("message_producer.messages", "joiner_for_llm")
 63  p.connect("joiner_for_llm", "llm")
 64  p.connect("llm.replies", "schema_validator.messages")
 65  p.connect("schema_validator.validation_error", "joiner_for_llm")
 66  
 67  result = p.run(data={
 68      "message_producer": {
 69          "messages":[ChatMessage.from_user("Generate JSON for person with name 'John' and age 30")]},
 70          "schema_validator": {
 71              "json_schema": {
 72                  "type": "object",
 73                  "properties": {"name": {"type": "string"},
 74                  "age": {"type": "integer"}
 75              }
 76          }
 77      }
 78  })
 79  print(result)
 80  >> {'schema_validator': {'validated': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>,
 81  _content=[TextContent(text="\n{\n  "name": "John",\n  "age": 30\n}")],
 82  _name=None, _meta={'model': 'gpt-4-1106-preview', 'index': 0,
 83  'finish_reason': 'stop', 'usage': {'completion_tokens': 17, 'prompt_tokens': 20, 'total_tokens': 37}})]}}
 84  ```
 85  
 86  #### __init__
 87  
 88  ```python
 89  __init__(
 90      json_schema: dict[str, Any] | None = None, error_template: str | None = None
 91  )
 92  ```
 93  
 94  Initialize the JsonSchemaValidator component.
 95  
 96  **Parameters:**
 97  
 98  - **json_schema** (<code>dict\[str, Any\] | None</code>) – A dictionary representing the [JSON schema](https://json-schema.org/) against which
 99    the messages' content is validated.
100  - **error_template** (<code>str | None</code>) – A custom template string for formatting the error message in case of validation failure.
101  
102  #### run
103  
104  ```python
105  run(
106      messages: list[ChatMessage],
107      json_schema: dict[str, Any] | None = None,
108      error_template: str | None = None,
109  ) -> dict[str, list[ChatMessage]]
110  ```
111  
112  Validates the last of the provided messages against the specified json schema.
113  
114  If it does, the message is passed along the "validated" output. If it does not, the message is passed along
115  the "validation_error" output.
116  
117  **Parameters:**
118  
119  - **messages** (<code>list\[ChatMessage\]</code>) – A list of ChatMessage instances to be validated. The last message in this list is the one
120    that is validated.
121  - **json_schema** (<code>dict\[str, Any\] | None</code>) – A dictionary representing the [JSON schema](https://json-schema.org/)
122    against which the messages' content is validated. If not provided, the schema from the component init
123    is used.
124  - **error_template** (<code>str | None</code>) – A custom template string for formatting the error message in case of validation. If not
125    provided, the `error_template` from the component init is used.
126  
127  **Returns:**
128  
129  - <code>dict\[str, list\[ChatMessage\]\]</code> – A dictionary with the following keys:
130  - "validated": A list of messages if the last message is valid.
131  - "validation_error": A list of messages if the last message is invalid.
132  
133  **Raises:**
134  
135  - <code>ValueError</code> – If no JSON schema is provided or if the message content is not a dictionary or a list of
136    dictionaries.