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.components.generators.chat import OpenAIChatGenerator
from haystack.components.others import BranchJoiner
from haystack.components.validators import JsonSchemaValidator
from haystack import component
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: