• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

deepset-ai / haystack / 15165238383

21 May 2025 02:42PM UTC coverage: 90.404% (-0.04%) from 90.443%
15165238383

Pull #9275

github

web-flow
Merge 82e69fe2c into 17432f710
Pull Request #9275: feat: return common type in SuperComponent type compatibility check

11135 of 12317 relevant lines covered (90.4%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.73
haystack/components/validators/json_schema.py
1
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
2
#
3
# SPDX-License-Identifier: Apache-2.0
4

5
import json
1✔
6
from typing import Any, Dict, List, Optional
1✔
7

8
from jsonschema import ValidationError, validate
1✔
9

10
from haystack import component
1✔
11
from haystack.dataclasses import ChatMessage
1✔
12

13

14
def is_valid_json(s: str) -> bool:
1✔
15
    """
16
    Check if the provided string is a valid JSON.
17

18
    :param s: The string to be checked.
19
    :returns: `True` if the string is a valid JSON; otherwise, `False`.
20
    """
21
    try:
1✔
22
        json.loads(s)
1✔
23
    except ValueError:
×
24
        return False
×
25
    return True
1✔
26

27

28
@component
1✔
29
class JsonSchemaValidator:
1✔
30
    """
31
    Validates JSON content of `ChatMessage` against a specified [JSON Schema](https://json-schema.org/).
32

33
    If JSON content of a message conforms to the provided schema, the message is passed along the "validated" output.
34
    If the JSON content does not conform to the schema, the message is passed along the "validation_error" output.
35
    In the latter case, the error message is constructed using the provided `error_template` or a default template.
36
    These error ChatMessages can be used by LLMs in Haystack 2.x recovery loops.
37

38
    Usage example:
39

40
    ```python
41
    from typing import List
42

43
    from haystack import Pipeline
44
    from haystack.components.generators.chat import OpenAIChatGenerator
45
    from haystack.components.joiners import BranchJoiner
46
    from haystack.components.validators import JsonSchemaValidator
47
    from haystack import component
48
    from haystack.dataclasses import ChatMessage
49

50

51
    @component
52
    class MessageProducer:
53

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

58

59
    p = Pipeline()
60
    p.add_component("llm", OpenAIChatGenerator(model="gpt-4-1106-preview",
61
                                               generation_kwargs={"response_format": {"type": "json_object"}}))
62
    p.add_component("schema_validator", JsonSchemaValidator())
63
    p.add_component("joiner_for_llm", BranchJoiner(List[ChatMessage]))
64
    p.add_component("message_producer", MessageProducer())
65

66
    p.connect("message_producer.messages", "joiner_for_llm")
67
    p.connect("joiner_for_llm", "llm")
68
    p.connect("llm.replies", "schema_validator.messages")
69
    p.connect("schema_validator.validation_error", "joiner_for_llm")
70

71
    result = p.run(data={
72
        "message_producer": {
73
            "messages":[ChatMessage.from_user("Generate JSON for person with name 'John' and age 30")]},
74
            "schema_validator": {
75
                "json_schema": {
76
                    "type": "object",
77
                    "properties": {"name": {"type": "string"},
78
                    "age": {"type": "integer"}
79
                }
80
            }
81
        }
82
    })
83
    print(result)
84
    >> {'schema_validator': {'validated': [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>,
85
    _content=[TextContent(text="\\n{\\n  "name": "John",\\n  "age": 30\\n}")],
86
    _name=None, _meta={'model': 'gpt-4-1106-preview', 'index': 0,
87
    'finish_reason': 'stop', 'usage': {'completion_tokens': 17, 'prompt_tokens': 20, 'total_tokens': 37}})]}}
88
    ```
89
    """
90

91
    # Default error description template
92
    default_error_template = (
1✔
93
        "The following generated JSON does not conform to the provided schema.\n"
94
        "Generated JSON: {failing_json}\n"
95
        "Error details:\n- Message: {error_message}\n"
96
        "- Error Path in JSON: {error_path}\n"
97
        "- Schema Path: {error_schema_path}\n"
98
        "Please match the following schema:\n"
99
        "{json_schema}\n"
100
        "and provide the corrected JSON content ONLY. Please do not output anything else than the raw corrected "
101
        "JSON string, this is the most important part of the task. Don't use any markdown and don't add any comment."
102
    )
103

104
    def __init__(self, json_schema: Optional[Dict[str, Any]] = None, error_template: Optional[str] = None):
1✔
105
        """
106
        Initialize the JsonSchemaValidator component.
107

108
        :param json_schema: A dictionary representing the [JSON schema](https://json-schema.org/) against which
109
            the messages' content is validated.
110
        :param error_template: A custom template string for formatting the error message in case of validation failure.
111
        """
112
        self.json_schema = json_schema
1✔
113
        self.error_template = error_template
1✔
114

115
    @component.output_types(validated=List[ChatMessage], validation_error=List[ChatMessage])
1✔
116
    def run(
1✔
117
        self,
118
        messages: List[ChatMessage],
119
        json_schema: Optional[Dict[str, Any]] = None,
120
        error_template: Optional[str] = None,
121
    ) -> Dict[str, List[ChatMessage]]:
122
        """
123
        Validates the last of the provided messages against the specified json schema.
124

125
        If it does, the message is passed along the "validated" output. If it does not, the message is passed along
126
        the "validation_error" output.
127

128
        :param messages: A list of ChatMessage instances to be validated. The last message in this list is the one
129
            that is validated.
130
        :param json_schema: A dictionary representing the [JSON schema](https://json-schema.org/)
131
            against which the messages' content is validated. If not provided, the schema from the component init
132
            is used.
133
        :param error_template: A custom template string for formatting the error message in case of validation. If not
134
            provided, the `error_template` from the component init is used.
135
        :return:  A dictionary with the following keys:
136
            - "validated": A list of messages if the last message is valid.
137
            - "validation_error": A list of messages if the last message is invalid.
138
        :raises ValueError: If no JSON schema is provided or if the message content is not a dictionary or a list of
139
            dictionaries.
140
        """
141
        last_message = messages[-1]
1✔
142
        if last_message.text is None:
1✔
143
            raise ValueError(f"The provided ChatMessage has no text. ChatMessage: {last_message}")
×
144
        if not is_valid_json(last_message.text):
1✔
145
            return {
×
146
                "validation_error": [
147
                    ChatMessage.from_user(
148
                        f"The message '{last_message.text}' is not a valid JSON object. "
149
                        f"Please provide only a valid JSON object in string format."
150
                        f"Don't use any markdown and don't add any comment."
151
                    )
152
                ]
153
            }
154

155
        last_message_content = json.loads(last_message.text)
1✔
156
        json_schema = json_schema or self.json_schema
1✔
157
        error_template = error_template or self.error_template or self.default_error_template
1✔
158

159
        if not json_schema:
1✔
160
            raise ValueError("Provide a JSON schema for validation either in the run method or in the component init.")
×
161
        # fc payload is json object but subtree `parameters` is string - we need to convert to json object
162
        # we need complete json to validate it against schema
163
        last_message_json = self._recursive_json_to_object(last_message_content)
1✔
164
        using_openai_schema: bool = self._is_openai_function_calling_schema(json_schema)
1✔
165
        if using_openai_schema:
1✔
166
            validation_schema = json_schema["parameters"]
1✔
167
        else:
168
            validation_schema = json_schema
1✔
169
        try:
1✔
170
            last_message_json = [last_message_json] if not isinstance(last_message_json, list) else last_message_json
1✔
171
            for content in last_message_json:
1✔
172
                if using_openai_schema:
1✔
173
                    validate(instance=content["function"]["arguments"], schema=validation_schema)
1✔
174
                else:
175
                    validate(instance=content, schema=validation_schema)
1✔
176

177
            return {"validated": [last_message]}
1✔
178
        except ValidationError as e:
1✔
179
            error_path = " -> ".join(map(str, e.absolute_path)) if e.absolute_path else "N/A"
1✔
180
            error_schema_path = " -> ".join(map(str, e.absolute_schema_path)) if e.absolute_schema_path else "N/A"
1✔
181

182
            error_template = error_template or self.default_error_template
1✔
183

184
            recovery_prompt = self._construct_error_recovery_message(
1✔
185
                error_template, str(e), error_path, error_schema_path, validation_schema, failing_json=last_message.text
186
            )
187
            return {"validation_error": [ChatMessage.from_user(recovery_prompt)]}
1✔
188

189
    def _construct_error_recovery_message(  # pylint: disable=too-many-positional-arguments
1✔
190
        self,
191
        error_template: str,
192
        error_message: str,
193
        error_path: str,
194
        error_schema_path: str,
195
        json_schema: Dict[str, Any],
196
        failing_json: str,
197
    ) -> str:
198
        """
199
        Constructs an error recovery message using a specified template or the default one if none is provided.
200

201
        :param error_template: A custom template string for formatting the error message in case of validation failure.
202
        :param error_message: The error message returned by the JSON schema validator.
203
        :param error_path: The path in the JSON content where the error occurred.
204
        :param error_schema_path: The path in the JSON schema where the error occurred.
205
        :param json_schema: The JSON schema against which the content is validated.
206
        :param failing_json: The generated invalid JSON string.
207
        """
208
        error_template = error_template or self.default_error_template
1✔
209

210
        return error_template.format(
1✔
211
            error_message=error_message,
212
            error_path=error_path,
213
            error_schema_path=error_schema_path,
214
            json_schema=json_schema,
215
            failing_json=failing_json,
216
        )
217

218
    def _is_openai_function_calling_schema(self, json_schema: Dict[str, Any]) -> bool:
1✔
219
        """
220
        Checks if the provided schema is a valid OpenAI function calling schema.
221

222
        :param json_schema: The JSON schema to check
223
        :return: `True` if the schema is a valid OpenAI function calling schema; otherwise, `False`.
224
        """
225
        return all(key in json_schema for key in ["name", "description", "parameters"])
1✔
226

227
    def _recursive_json_to_object(self, data: Any) -> Any:
1✔
228
        """
229
        Convert any string values that are valid JSON objects into dictionary objects.
230

231
        Returns a new data structure.
232

233
        :param data: The data structure to be traversed.
234
        :return: A new data structure with JSON strings converted to dictionary objects.
235
        """
236
        if isinstance(data, list):
1✔
237
            return [self._recursive_json_to_object(item) for item in data]
1✔
238

239
        if isinstance(data, dict):
1✔
240
            new_dict = {}
1✔
241
            for key, value in data.items():
1✔
242
                if isinstance(value, str):
1✔
243
                    try:
1✔
244
                        json_value = json.loads(value)
1✔
245
                        if isinstance(json_value, (dict, list)):
1✔
246
                            new_dict[key] = self._recursive_json_to_object(json_value)
1✔
247
                        else:
248
                            new_dict[key] = value  # Preserve the original string value
×
249
                    except json.JSONDecodeError:
1✔
250
                        new_dict[key] = value
1✔
251
                elif isinstance(value, dict):
1✔
252
                    new_dict[key] = self._recursive_json_to_object(value)
1✔
253
                else:
254
                    new_dict[key] = value
×
255
            return new_dict
1✔
256

257
        # If it's neither a list nor a dictionary, return the value directly
258
        raise ValueError("Input must be a dictionary or a list of dictionaries.")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc