DocumentationAPI ReferenceπŸ““ TutorialsπŸ§‘β€πŸ³ Cookbook🀝 IntegrationsπŸ’œ Discord

JsonSchemaValidator

Use this component to ensure that an LLM-generated chat message JSON adheres to a specific schema.

NameJsonSchemaValidator
Folder path/validators/
Most common position in a pipelineAfter a Generator
Mandatory input variablesβ€œmessages”: A list of ChatMessage instances to be validated – the last message in this list is the one that is validated
Output variablesβ€œvalidated”: A list of messages if the last message is valid

”validation_error”: A list of messages if the last message is invalid

Overview

JsonSchemaValidatorΒ checks the JSON content of aΒ ChatMessageΒ against a givenΒ JSON Schema. If a message's JSON content follows the provided schema, it's moved to theΒ validatedΒ output. If not, it's moved to theΒ validation_erroroutput. When there's an error, the component uses either the provided customΒ error_templateΒ or a default template to create the error message. These errorΒ ChatMessagesΒ can be used in Haystack recovery loops.

Usage

In a pipeline

In this simple pipeline, the MessageProducer sends a list of chat messages to a Generator through BranchJoiner. The resulting messages from the Generator are sent to JsonSchemaValidator, and the error ChatMessages are sent back to BranchJoiner for a recovery loop.

from typing import List

from haystack import Pipeline
from haystack import component
from haystack.components.generators.chat import OpenAIChatGenerator
from haystack.components.joiners import BranchJoiner
from haystack.components.validators import JsonSchemaValidator
from haystack.dataclasses import ChatMessage


@component
class MessageProducer:

    @component.output_types(messages=List[ChatMessage])
    def run(self, messages: List[ChatMessage]) -> dict:
        return {"messages": messages}


p = Pipeline()
p.add_component("llm", OpenAIChatGenerator(model="gpt-4-1106-preview",
                                           generation_kwargs={"response_format": {"type": "json_object"}}))
p.add_component("schema_validator", JsonSchemaValidator())
p.add_component("branch_joiner", BranchJoiner(List[ChatMessage]))
p.add_component("message_producer", MessageProducer())

p.connect("message_producer.messages", "branch_joiner")
p.connect("branch_joiner", "llm")
p.connect("llm.replies", "schema_validator.messages")
p.connect("schema_validator.validation_error", "branch_joiner")

result = p.run(
    data={"message_producer": {
        "messages": [ChatMessage.from_user("Generate JSON for person with name 'John' and age 30")]},
          "schema_validator": {"json_schema": {"type": "object",
                                               "properties": {"name": {"type": "string"},
                                                              "age": {"type": "integer"}}}}})
print(result)

>> {'schema_validator': {'validated': [ChatMessage(content='\n{\n  "name": "John",\n  "age": 30\n}',
role=<ChatRole.ASSISTANT: 'assistant'>, name=None, meta={'model': 'gpt-4-1106-preview', 'index': 0,
'finish_reason': 'stop', 'usage': {'completion_tokens': 17, 'prompt_tokens': 20, 'total_tokens': 37}})]}}

Related Links

See the parameters details in our API reference: