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

deepset-ai / haystack / 17296086229

28 Aug 2025 12:39PM UTC coverage: 92.108%. Remained the same
17296086229

Pull #9748

github

web-flow
Merge 239e311fb into 41b7ed4f4
Pull Request #9748: fix: reintroduce helpful error message in `ChatMessage` deserialization

12909 of 14015 relevant lines covered (92.11%)

0.92 hits per line

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

99.33
haystack/dataclasses/chat_message.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 dataclasses import asdict, dataclass, field
1✔
7
from enum import Enum
1✔
8
from typing import Any, Optional, Sequence, Union
1✔
9

10
from haystack import logging
1✔
11
from haystack.dataclasses.image_content import ImageContent
1✔
12

13
logger = logging.getLogger(__name__)
1✔
14

15

16
LEGACY_INIT_PARAMETERS = {"role", "content", "meta", "name"}
1✔
17

18

19
class ChatRole(str, Enum):
1✔
20
    """
21
    Enumeration representing the roles within a chat.
22
    """
23

24
    #: The user role. A message from the user contains only text.
25
    USER = "user"
1✔
26

27
    #: The system role. A message from the system contains only text.
28
    SYSTEM = "system"
1✔
29

30
    #: The assistant role. A message from the assistant can contain text and Tool calls. It can also store metadata.
31
    ASSISTANT = "assistant"
1✔
32

33
    #: The tool role. A message from a tool contains the result of a Tool invocation.
34
    TOOL = "tool"
1✔
35

36
    @staticmethod
1✔
37
    def from_str(string: str) -> "ChatRole":
1✔
38
        """
39
        Convert a string to a ChatRole enum.
40
        """
41
        enum_map = {e.value: e for e in ChatRole}
1✔
42
        role = enum_map.get(string)
1✔
43
        if role is None:
1✔
44
            msg = f"Unknown chat role '{string}'. Supported roles are: {list(enum_map.keys())}"
1✔
45
            raise ValueError(msg)
1✔
46
        return role
1✔
47

48

49
@dataclass
1✔
50
class ToolCall:
1✔
51
    """
52
    Represents a Tool call prepared by the model, usually contained in an assistant message.
53

54
    :param id: The ID of the Tool call.
55
    :param tool_name: The name of the Tool to call.
56
    :param arguments: The arguments to call the Tool with.
57
    """
58

59
    tool_name: str
1✔
60
    arguments: dict[str, Any]
1✔
61
    id: Optional[str] = None  # noqa: A003
1✔
62

63
    def to_dict(self) -> dict[str, Any]:
1✔
64
        """
65
        Convert ToolCall into a dictionary.
66

67
        :returns: A dictionary with keys 'tool_name', 'arguments', and 'id'.
68
        """
69
        return asdict(self)
1✔
70

71
    @classmethod
1✔
72
    def from_dict(cls, data: dict[str, Any]) -> "ToolCall":
1✔
73
        """
74
        Creates a new ToolCall object from a dictionary.
75

76
        :param data:
77
            The dictionary to build the ToolCall object.
78
        :returns:
79
            The created object.
80
        """
81
        return ToolCall(**data)
1✔
82

83

84
@dataclass
1✔
85
class ToolCallResult:
1✔
86
    """
87
    Represents the result of a Tool invocation.
88

89
    :param result: The result of the Tool invocation.
90
    :param origin: The Tool call that produced this result.
91
    :param error: Whether the Tool invocation resulted in an error.
92
    """
93

94
    result: str
1✔
95
    origin: ToolCall
1✔
96
    error: bool
1✔
97

98
    def to_dict(self) -> dict[str, Any]:
1✔
99
        """
100
        Converts ToolCallResult into a dictionary.
101

102
        :returns: A dictionary with keys 'result', 'origin', and 'error'.
103
        """
104
        return asdict(self)
1✔
105

106
    @classmethod
1✔
107
    def from_dict(cls, data: dict[str, Any]) -> "ToolCallResult":
1✔
108
        """
109
        Creates a ToolCallResult from a dictionary.
110

111
        :param data:
112
            The dictionary to build the ToolCallResult object.
113
        :returns:
114
            The created object.
115
        """
116
        if not all(x in data for x in ["result", "origin", "error"]):
1✔
117
            raise ValueError(
1✔
118
                "Fields `result`, `origin`, `error` are required for ToolCallResult deserialization. "
119
                f"Received dictionary with keys {list(data.keys())}"
120
            )
121
        return ToolCallResult(result=data["result"], origin=ToolCall.from_dict(data["origin"]), error=data["error"])
1✔
122

123

124
@dataclass
1✔
125
class TextContent:
1✔
126
    """
127
    The textual content of a chat message.
128

129
    :param text: The text content of the message.
130
    """
131

132
    text: str
1✔
133

134
    def to_dict(self) -> dict[str, Any]:
1✔
135
        """
136
        Convert TextContent into a dictionary.
137
        """
138
        return asdict(self)
1✔
139

140
    @classmethod
1✔
141
    def from_dict(cls, data: dict[str, Any]) -> "TextContent":
1✔
142
        """
143
        Create a TextContent from a dictionary.
144
        """
145
        return TextContent(**data)
1✔
146

147

148
@dataclass
1✔
149
class ReasoningContent:
1✔
150
    """
151
    Represents the optional reasoning content prepared by the model, usually contained in an assistant message.
152

153
    :param reasoning_text: The reasoning text produced by the model.
154
    :param extra: Dictionary of extra information about the reasoning content. Use to store provider-specific
155
        information. To avoid serialization issues, values should be JSON serializable.
156
    """
157

158
    reasoning_text: str
1✔
159
    extra: dict[str, Any] = field(default_factory=dict)
1✔
160

161
    def to_dict(self) -> dict[str, Any]:
1✔
162
        """
163
        Convert ReasoningContent into a dictionary.
164

165
        :returns: A dictionary with keys 'reasoning_text', and 'extra'.
166
        """
167
        return asdict(self)
1✔
168

169
    @classmethod
1✔
170
    def from_dict(cls, data: dict[str, Any]) -> "ReasoningContent":
1✔
171
        """
172
        Creates a new ReasoningContent object from a dictionary.
173

174
        :param data:
175
            The dictionary to build the ReasoningContent object.
176
        :returns:
177
            The created object.
178
        """
179
        return ReasoningContent(**data)
1✔
180

181

182
ChatMessageContentT = Union[TextContent, ToolCall, ToolCallResult, ImageContent, ReasoningContent]
1✔
183

184
_CONTENT_PART_CLASSES_TO_SERIALIZATION_KEYS: dict[type[ChatMessageContentT], str] = {
1✔
185
    TextContent: "text",
186
    ToolCall: "tool_call",
187
    ToolCallResult: "tool_call_result",
188
    ImageContent: "image",
189
    ReasoningContent: "reasoning",
190
}
191

192

193
def _deserialize_content_part(part: dict[str, Any]) -> ChatMessageContentT:
1✔
194
    """
195
    Deserialize a single content part of a serialized ChatMessage.
196

197
    :param part:
198
        A dictionary representing a single content part of a serialized ChatMessage.
199
    :returns:
200
        A ChatMessageContentT object.
201
    :raises ValueError:
202
        If the part is not a valid ChatMessageContentT object.
203
    """
204
    # handle flat text format separately
205
    if "text" in part:
1✔
206
        return TextContent.from_dict(part)
1✔
207

208
    for cls, serialization_key in _CONTENT_PART_CLASSES_TO_SERIALIZATION_KEYS.items():
1✔
209
        if serialization_key in part:
1✔
210
            return cls.from_dict(part[serialization_key])
1✔
211

212
    # NOTE: this verbose error message provides guidance to LLMs when creating invalid messages during agent runs
213
    msg = (
1✔
214
        f"Unsupported content part in the serialized ChatMessage: {part}. "
215
        "The `content` field of the serialized ChatMessage must be a list of dictionaries, where each "
216
        "dictionary contains one of these keys: 'text', 'image', 'reasoning', 'tool_call', or 'tool_call_result'. "
217
        "Valid formats: [{'text': 'Hello'}, {'image': {'base64_image': '...', ...}}, "
218
        "{'reasoning': {'reasoning_text': 'I think...', 'extra': {...}}}, "
219
        "{'tool_call': {'tool_name': 'search', 'arguments': {}, 'id': 'call_123'}}, "
220
        "{'tool_call_result': {'result': 'data', 'origin': {...}, 'error': false}}]"
221
    )
222
    raise ValueError(msg)
1✔
223

224

225
def _serialize_content_part(part: ChatMessageContentT) -> dict[str, Any]:
1✔
226
    """
227
    Serialize a single content part of a ChatMessage.
228

229
    :param part:
230
        A ChatMessageContentT object.
231
    :returns:
232
        A dictionary representing the content part.
233
    :raises TypeError:
234
        If the part is not a valid ChatMessageContentT object.
235
    """
236
    serialization_key = _CONTENT_PART_CLASSES_TO_SERIALIZATION_KEYS.get(type(part))
1✔
237
    if serialization_key is None:
1✔
238
        raise TypeError(f"Unsupported type in ChatMessage content: `{type(part).__name__}` for `{part}`.")
1✔
239

240
    # handle flat text format separately
241
    if isinstance(part, TextContent):
1✔
242
        return part.to_dict()
1✔
243

244
    return {serialization_key: part.to_dict()}
1✔
245

246

247
@dataclass
1✔
248
class ChatMessage:  # pylint: disable=too-many-public-methods # it's OK since we expose several properties
1✔
249
    """
250
    Represents a message in a LLM chat conversation.
251

252
    Use the `from_assistant`, `from_user`, `from_system`, and `from_tool` class methods to create a ChatMessage.
253
    """
254

255
    _role: ChatRole
1✔
256
    _content: Sequence[ChatMessageContentT]
1✔
257
    _name: Optional[str] = None
1✔
258
    _meta: dict[str, Any] = field(default_factory=dict, hash=False)
1✔
259

260
    def __new__(cls, *args, **kwargs):
1✔
261
        """
262
        This method is reimplemented to make the changes to the `ChatMessage` dataclass more visible.
263
        """
264

265
        general_msg = (
1✔
266
            "Use the `from_assistant`, `from_user`, `from_system`, and `from_tool` class methods to create a "
267
            "ChatMessage. For more information about the new API and how to migrate, see the documentation:"
268
            " https://docs.haystack.deepset.ai/docs/chatmessage"
269
        )
270

271
        if any(param in kwargs for param in LEGACY_INIT_PARAMETERS):
1✔
272
            raise TypeError(
1✔
273
                "The `role`, `content`, `meta`, and `name` init parameters of `ChatMessage` have been removed. "
274
                f"{general_msg}"
275
            )
276

277
        return super(ChatMessage, cls).__new__(cls)
1✔
278

279
    def __getattribute__(self, name):
1✔
280
        """
281
        This method is reimplemented to make the `content` attribute removal more visible.
282
        """
283

284
        if name == "content":
1✔
285
            msg = (
1✔
286
                "The `content` attribute of `ChatMessage` has been removed. "
287
                "Use the `text` property to access the textual value. "
288
                "For more information about the new API and how to migrate, see the documentation: "
289
                "https://docs.haystack.deepset.ai/docs/chatmessage"
290
            )
291
            raise AttributeError(msg)
1✔
292
        return object.__getattribute__(self, name)
1✔
293

294
    def __len__(self):
1✔
295
        return len(self._content)
1✔
296

297
    @property
1✔
298
    def role(self) -> ChatRole:
1✔
299
        """
300
        Returns the role of the entity sending the message.
301
        """
302
        return self._role
1✔
303

304
    @property
1✔
305
    def meta(self) -> dict[str, Any]:
1✔
306
        """
307
        Returns the metadata associated with the message.
308
        """
309
        return self._meta
1✔
310

311
    @property
1✔
312
    def name(self) -> Optional[str]:
1✔
313
        """
314
        Returns the name associated with the message.
315
        """
316
        return self._name
1✔
317

318
    @property
1✔
319
    def texts(self) -> list[str]:
1✔
320
        """
321
        Returns the list of all texts contained in the message.
322
        """
323
        return [content.text for content in self._content if isinstance(content, TextContent)]
1✔
324

325
    @property
1✔
326
    def text(self) -> Optional[str]:
1✔
327
        """
328
        Returns the first text contained in the message.
329
        """
330
        if texts := self.texts:
1✔
331
            return texts[0]
1✔
332
        return None
1✔
333

334
    @property
1✔
335
    def tool_calls(self) -> list[ToolCall]:
1✔
336
        """
337
        Returns the list of all Tool calls contained in the message.
338
        """
339
        return [content for content in self._content if isinstance(content, ToolCall)]
1✔
340

341
    @property
1✔
342
    def tool_call(self) -> Optional[ToolCall]:
1✔
343
        """
344
        Returns the first Tool call contained in the message.
345
        """
346
        if tool_calls := self.tool_calls:
1✔
347
            return tool_calls[0]
1✔
348
        return None
1✔
349

350
    @property
1✔
351
    def tool_call_results(self) -> list[ToolCallResult]:
1✔
352
        """
353
        Returns the list of all Tool call results contained in the message.
354
        """
355
        return [content for content in self._content if isinstance(content, ToolCallResult)]
1✔
356

357
    @property
1✔
358
    def tool_call_result(self) -> Optional[ToolCallResult]:
1✔
359
        """
360
        Returns the first Tool call result contained in the message.
361
        """
362
        if tool_call_results := self.tool_call_results:
1✔
363
            return tool_call_results[0]
1✔
364
        return None
1✔
365

366
    @property
1✔
367
    def images(self) -> list[ImageContent]:
1✔
368
        """
369
        Returns the list of all images contained in the message.
370
        """
371
        return [content for content in self._content if isinstance(content, ImageContent)]
1✔
372

373
    @property
1✔
374
    def image(self) -> Optional[ImageContent]:
1✔
375
        """
376
        Returns the first image contained in the message.
377
        """
378
        if images := self.images:
1✔
379
            return images[0]
×
380
        return None
1✔
381

382
    @property
1✔
383
    def reasonings(self) -> list[ReasoningContent]:
1✔
384
        """
385
        Returns the list of all reasoning contents contained in the message.
386
        """
387
        return [content for content in self._content if isinstance(content, ReasoningContent)]
1✔
388

389
    @property
1✔
390
    def reasoning(self) -> Optional[ReasoningContent]:
1✔
391
        """
392
        Returns the first reasoning content contained in the message.
393
        """
394
        if reasonings := self.reasonings:
1✔
395
            return reasonings[0]
1✔
396
        return None
1✔
397

398
    def is_from(self, role: Union[ChatRole, str]) -> bool:
1✔
399
        """
400
        Check if the message is from a specific role.
401

402
        :param role: The role to check against.
403
        :returns: True if the message is from the specified role, False otherwise.
404
        """
405
        if isinstance(role, str):
1✔
406
            role = ChatRole.from_str(role)
1✔
407
        return self._role == role
1✔
408

409
    @classmethod
1✔
410
    def from_user(
1✔
411
        cls,
412
        text: Optional[str] = None,
413
        meta: Optional[dict[str, Any]] = None,
414
        name: Optional[str] = None,
415
        *,
416
        content_parts: Optional[Sequence[Union[TextContent, str, ImageContent]]] = None,
417
    ) -> "ChatMessage":
418
        """
419
        Create a message from the user.
420

421
        :param text: The text content of the message. Specify this or content_parts.
422
        :param meta: Additional metadata associated with the message.
423
        :param name: An optional name for the participant. This field is only supported by OpenAI.
424
        :param content_parts: A list of content parts to include in the message. Specify this or text.
425
        :returns: A new ChatMessage instance.
426
        """
427
        if text is None and content_parts is None:
1✔
428
            raise ValueError("Either text or content_parts must be provided.")
1✔
429
        if text is not None and content_parts is not None:
1✔
430
            raise ValueError("Only one of text or content_parts can be provided.")
1✔
431

432
        content: list[Union[TextContent, ImageContent]] = []
1✔
433

434
        if text is not None:
1✔
435
            content = [TextContent(text=text)]
1✔
436
        elif content_parts is not None:
1✔
437
            for part in content_parts:
1✔
438
                if isinstance(part, str):
1✔
439
                    content.append(TextContent(text=part))
1✔
440
                elif isinstance(part, (TextContent, ImageContent)):
1✔
441
                    content.append(part)
1✔
442
                else:
443
                    raise ValueError(
1✔
444
                        f"The user message must contain only text or image parts. Unsupported part: {part}"
445
                    )
446
            if len(content) == 0:
1✔
447
                raise ValueError("The user message must contain at least one textual or image part.")
1✔
448

449
        return cls(_role=ChatRole.USER, _content=content, _meta=meta or {}, _name=name)
1✔
450

451
    @classmethod
1✔
452
    def from_system(cls, text: str, meta: Optional[dict[str, Any]] = None, name: Optional[str] = None) -> "ChatMessage":
1✔
453
        """
454
        Create a message from the system.
455

456
        :param text: The text content of the message.
457
        :param meta: Additional metadata associated with the message.
458
        :param name: An optional name for the participant. This field is only supported by OpenAI.
459
        :returns: A new ChatMessage instance.
460
        """
461
        return cls(_role=ChatRole.SYSTEM, _content=[TextContent(text=text)], _meta=meta or {}, _name=name)
1✔
462

463
    @classmethod
1✔
464
    def from_assistant(
1✔
465
        cls,
466
        text: Optional[str] = None,
467
        meta: Optional[dict[str, Any]] = None,
468
        name: Optional[str] = None,
469
        tool_calls: Optional[list[ToolCall]] = None,
470
        *,
471
        reasoning: Optional[Union[str, ReasoningContent]] = None,
472
    ) -> "ChatMessage":
473
        """
474
        Create a message from the assistant.
475

476
        :param text: The text content of the message.
477
        :param meta: Additional metadata associated with the message.
478
        :param name: An optional name for the participant. This field is only supported by OpenAI.
479
        :param tool_calls: The Tool calls to include in the message.
480
        :param reasoning: The reasoning content to include in the message.
481
        :returns: A new ChatMessage instance.
482
        """
483
        content: list[ChatMessageContentT] = []
1✔
484
        if reasoning:
1✔
485
            if isinstance(reasoning, str):
1✔
486
                content.append(ReasoningContent(reasoning_text=reasoning))
1✔
487
            elif isinstance(reasoning, ReasoningContent):
1✔
488
                content.append(reasoning)
1✔
489
            else:
490
                raise TypeError(f"reasoning must be a string or a ReasoningContent object, got {type(reasoning)}")
1✔
491
        if text is not None:
1✔
492
            content.append(TextContent(text=text))
1✔
493
        if tool_calls:
1✔
494
            content.extend(tool_calls)
1✔
495

496
        return cls(_role=ChatRole.ASSISTANT, _content=content, _meta=meta or {}, _name=name)
1✔
497

498
    @classmethod
1✔
499
    def from_tool(
1✔
500
        cls, tool_result: str, origin: ToolCall, error: bool = False, meta: Optional[dict[str, Any]] = None
501
    ) -> "ChatMessage":
502
        """
503
        Create a message from a Tool.
504

505
        :param tool_result: The result of the Tool invocation.
506
        :param origin: The Tool call that produced this result.
507
        :param error: Whether the Tool invocation resulted in an error.
508
        :param meta: Additional metadata associated with the message.
509
        :returns: A new ChatMessage instance.
510
        """
511
        return cls(
1✔
512
            _role=ChatRole.TOOL,
513
            _content=[ToolCallResult(result=tool_result, origin=origin, error=error)],
514
            _meta=meta or {},
515
        )
516

517
    def to_dict(self) -> dict[str, Any]:
1✔
518
        """
519
        Converts ChatMessage into a dictionary.
520

521
        :returns:
522
            Serialized version of the object.
523
        """
524

525
        serialized: dict[str, Any] = {}
1✔
526
        serialized["role"] = self._role.value
1✔
527
        serialized["meta"] = self._meta
1✔
528
        serialized["name"] = self._name
1✔
529

530
        serialized["content"] = [_serialize_content_part(part) for part in self._content]
1✔
531
        return serialized
1✔
532

533
    @classmethod
1✔
534
    def from_dict(cls, data: dict[str, Any]) -> "ChatMessage":
1✔
535
        """
536
        Creates a new ChatMessage object from a dictionary.
537

538
        :param data:
539
            The dictionary to build the ChatMessage object.
540
        :returns:
541
            The created object.
542
        """
543

544
        # NOTE: this verbose error message provides guidance to LLMs when creating invalid messages during agent runs
545
        if not "role" in data and not "_role" in data:
1✔
546
            raise ValueError(
1✔
547
                "The `role` field is required in the message dictionary. "
548
                f"Expected a dictionary with 'role' field containing one of: {[role.value for role in ChatRole]}. "
549
                f"Common roles are 'user' (for user messages) and 'assistant' (for AI responses). "
550
                f"Received dictionary with keys: {list(data.keys())}"
551
            )
552

553
        if "content" in data:
1✔
554
            init_params: dict[str, Any] = {
1✔
555
                "_role": ChatRole(data["role"]),
556
                "_name": data.get("name"),
557
                "_meta": data.get("meta") or {},
558
            }
559

560
            if isinstance(data["content"], list):
1✔
561
                # current format - the serialized `content` field is a list of dictionaries
562
                init_params["_content"] = [_deserialize_content_part(part) for part in data["content"]]
1✔
563
            elif isinstance(data["content"], str):
1✔
564
                # pre 2.9.0 format - the `content` field is a string
565
                init_params["_content"] = [TextContent(text=data["content"])]
1✔
566
            else:
567
                raise TypeError(f"Unsupported content type in serialized ChatMessage: `{(data['content'])}`")
×
568
            return cls(**init_params)
1✔
569

570
        if "_content" in data:
1✔
571
            # format for versions >=2.9.0 and <2.12.0 - the serialized `_content` field is a list of dictionaries
572
            return cls(
1✔
573
                _role=ChatRole(data["_role"]),
574
                _content=[_deserialize_content_part(part) for part in data["_content"]],
575
                _name=data.get("_name"),
576
                _meta=data.get("_meta") or {},
577
            )
578

579
        raise ValueError(f"Missing 'content' or '_content' in serialized ChatMessage: `{data}`")
1✔
580

581
    def to_openai_dict_format(self, require_tool_call_ids: bool = True) -> dict[str, Any]:
1✔
582
        """
583
        Convert a ChatMessage to the dictionary format expected by OpenAI's Chat API.
584

585
        :param require_tool_call_ids:
586
            If True (default), enforces that each Tool Call includes a non-null `id` attribute.
587
            Set to False to allow Tool Calls without `id`, which may be suitable for shallow OpenAI-compatible APIs.
588
        :returns:
589
            The ChatMessage in the format expected by OpenAI's Chat API.
590

591
        :raises ValueError:
592
            If the message format is invalid, or if `require_tool_call_ids` is True and any Tool Call is missing an
593
            `id` attribute.
594
        """
595
        text_contents = self.texts
1✔
596
        tool_calls = self.tool_calls
1✔
597
        tool_call_results = self.tool_call_results
1✔
598
        images = self.images
1✔
599

600
        if not text_contents and not tool_calls and not tool_call_results and not images:
1✔
601
            raise ValueError(
1✔
602
                "A `ChatMessage` must contain at least one `TextContent`, `ToolCall`, "
603
                "`ToolCallResult`, or `ImageContent`."
604
            )
605
        if len(tool_call_results) > 0 and len(self._content) > 1:
1✔
606
            raise ValueError(
1✔
607
                "For OpenAI compatibility, a `ChatMessage` with a `ToolCallResult` cannot contain any other content."
608
            )
609

610
        openai_msg: dict[str, Any] = {"role": self._role.value}
1✔
611

612
        # Add name field if present
613
        if self._name is not None:
1✔
614
            openai_msg["name"] = self._name
1✔
615

616
        # user message
617
        if openai_msg["role"] == "user":
1✔
618
            if len(self._content) == 1 and isinstance(self._content[0], TextContent):
1✔
619
                openai_msg["content"] = self.text
1✔
620
                return openai_msg
1✔
621

622
            # if the user message contains a list of text and images, OpenAI expects a list of dictionaries
623
            content = []
1✔
624
            for part in self._content:
1✔
625
                if isinstance(part, TextContent):
1✔
626
                    content.append({"type": "text", "text": part.text})
1✔
627
                elif isinstance(part, ImageContent):
1✔
628
                    image_item: dict[str, Any] = {
1✔
629
                        "type": "image_url",
630
                        # If no MIME type is provided, default to JPEG.
631
                        # OpenAI API appears to tolerate MIME type mismatches.
632
                        "image_url": {"url": f"data:{part.mime_type or 'image/jpeg'};base64,{part.base64_image}"},
633
                    }
634
                    if part.detail:
1✔
635
                        image_item["image_url"]["detail"] = part.detail
1✔
636
                    content.append(image_item)
1✔
637
            openai_msg["content"] = content
1✔
638
            return openai_msg
1✔
639

640
        # tool message
641
        if tool_call_results:
1✔
642
            result = tool_call_results[0]
1✔
643
            openai_msg["content"] = result.result
1✔
644
            if result.origin.id is not None:
1✔
645
                openai_msg["tool_call_id"] = result.origin.id
1✔
646
            elif require_tool_call_ids:
1✔
647
                raise ValueError("`ToolCall` must have a non-null `id` attribute to be used with OpenAI.")
1✔
648
            # OpenAI does not provide a way to communicate errors in tool invocations, so we ignore the error field
649
            return openai_msg
1✔
650

651
        # system and assistant messages
652
        # OpenAI Chat Completions API does not support reasoning content, so we ignore it
653
        if text_contents:
1✔
654
            openai_msg["content"] = text_contents[0]
1✔
655
        if tool_calls:
1✔
656
            openai_tool_calls = []
1✔
657
            for tc in tool_calls:
1✔
658
                openai_tool_call = {
1✔
659
                    "type": "function",
660
                    # We disable ensure_ascii so special chars like emojis are not converted
661
                    "function": {"name": tc.tool_name, "arguments": json.dumps(tc.arguments, ensure_ascii=False)},
662
                }
663
                if tc.id is not None:
1✔
664
                    openai_tool_call["id"] = tc.id
1✔
665
                elif require_tool_call_ids:
1✔
666
                    raise ValueError("`ToolCall` must have a non-null `id` attribute to be used with OpenAI.")
1✔
667
                openai_tool_calls.append(openai_tool_call)
1✔
668
            openai_msg["tool_calls"] = openai_tool_calls
1✔
669
        return openai_msg
1✔
670

671
    @staticmethod
1✔
672
    def _validate_openai_message(message: dict[str, Any]) -> None:
1✔
673
        """
674
        Validate that a message dictionary follows OpenAI's Chat API format.
675

676
        :param message: The message dictionary to validate
677
        :raises ValueError: If the message format is invalid
678
        """
679
        if "role" not in message:
1✔
680
            raise ValueError("The `role` field is required in the message dictionary.")
1✔
681

682
        role = message["role"]
1✔
683
        content = message.get("content")
1✔
684
        tool_calls = message.get("tool_calls")
1✔
685

686
        if role not in ["assistant", "user", "system", "developer", "tool"]:
1✔
687
            raise ValueError(f"Unsupported role: {role}")
1✔
688

689
        if role == "assistant":
1✔
690
            if not content and not tool_calls:
1✔
691
                raise ValueError("For assistant messages, either `content` or `tool_calls` must be present.")
1✔
692
            if tool_calls:
1✔
693
                for tc in tool_calls:
1✔
694
                    if "function" not in tc:
1✔
695
                        raise ValueError("Tool calls must contain the `function` field")
1✔
696
        elif not content:
1✔
697
            raise ValueError(f"The `content` field is required for {role} messages.")
1✔
698

699
    @classmethod
1✔
700
    def from_openai_dict_format(cls, message: dict[str, Any]) -> "ChatMessage":
1✔
701
        """
702
        Create a ChatMessage from a dictionary in the format expected by OpenAI's Chat API.
703

704
        NOTE: While OpenAI's API requires `tool_call_id` in both tool calls and tool messages, this method
705
        accepts messages without it to support shallow OpenAI-compatible APIs.
706
        If you plan to use the resulting ChatMessage with OpenAI, you must include `tool_call_id` or you'll
707
        encounter validation errors.
708

709
        :param message:
710
            The OpenAI dictionary to build the ChatMessage object.
711
        :returns:
712
            The created ChatMessage object.
713

714
        :raises ValueError:
715
            If the message dictionary is missing required fields.
716
        """
717
        cls._validate_openai_message(message)
1✔
718

719
        role = message["role"]
1✔
720
        content = message.get("content")
1✔
721
        name = message.get("name")
1✔
722
        tool_calls = message.get("tool_calls")
1✔
723
        tool_call_id = message.get("tool_call_id")
1✔
724

725
        if role == "assistant":
1✔
726
            haystack_tool_calls = None
1✔
727
            if tool_calls:
1✔
728
                haystack_tool_calls = []
1✔
729
                for tc in tool_calls:
1✔
730
                    haystack_tc = ToolCall(
1✔
731
                        id=tc.get("id"),
732
                        tool_name=tc["function"]["name"],
733
                        arguments=json.loads(tc["function"]["arguments"]),
734
                    )
735
                    haystack_tool_calls.append(haystack_tc)
1✔
736
            return cls.from_assistant(text=content, name=name, tool_calls=haystack_tool_calls)
1✔
737

738
        assert content is not None  # ensured by _validate_openai_message, but we need to make mypy happy
1✔
739

740
        if role == "user":
1✔
741
            return cls.from_user(text=content, name=name)
1✔
742
        if role in ["system", "developer"]:
1✔
743
            return cls.from_system(text=content, name=name)
1✔
744

745
        return cls.from_tool(
1✔
746
            tool_result=content, origin=ToolCall(id=tool_call_id, tool_name="", arguments={}), error=False
747
        )
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