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

deepset-ai / haystack / 18592817487

17 Oct 2025 12:33PM UTC coverage: 92.2% (+0.1%) from 92.062%
18592817487

Pull #9859

github

web-flow
Merge f20ff2b98 into a43c47b63
Pull Request #9859: feat: Add FallbackChatGenerator

13346 of 14475 relevant lines covered (92.2%)

0.92 hits per line

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

96.76
haystack/components/generators/chat/openai.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
import os
1✔
7
from datetime import datetime
1✔
8
from typing import Any, Optional, Union
1✔
9

10
from openai import AsyncOpenAI, AsyncStream, OpenAI, Stream
1✔
11
from openai.lib._pydantic import to_strict_json_schema
1✔
12
from openai.types.chat import (
1✔
13
    ChatCompletion,
14
    ChatCompletionChunk,
15
    ChatCompletionMessage,
16
    ChatCompletionMessageCustomToolCall,
17
    ParsedChatCompletion,
18
    ParsedChatCompletionMessage,
19
)
20
from openai.types.chat.chat_completion import Choice
1✔
21
from openai.types.chat.chat_completion_chunk import Choice as ChunkChoice
1✔
22
from pydantic import BaseModel
1✔
23

24
from haystack import component, default_from_dict, default_to_dict, logging
1✔
25
from haystack.components.generators.utils import _convert_streaming_chunks_to_chat_message
1✔
26
from haystack.dataclasses import (
1✔
27
    AsyncStreamingCallbackT,
28
    ChatMessage,
29
    ComponentInfo,
30
    FinishReason,
31
    StreamingCallbackT,
32
    StreamingChunk,
33
    SyncStreamingCallbackT,
34
    ToolCall,
35
    ToolCallDelta,
36
    select_streaming_callback,
37
)
38
from haystack.tools import (
1✔
39
    Tool,
40
    Toolset,
41
    _check_duplicate_tool_names,
42
    deserialize_tools_or_toolset_inplace,
43
    serialize_tools_or_toolset,
44
)
45
from haystack.utils import Secret, deserialize_callable, deserialize_secrets_inplace, serialize_callable
1✔
46
from haystack.utils.http_client import init_http_client
1✔
47

48
logger = logging.getLogger(__name__)
1✔
49

50

51
@component
1✔
52
class OpenAIChatGenerator:
1✔
53
    """
54
    Completes chats using OpenAI's large language models (LLMs).
55

56
    It works with the gpt-4 and o-series models and supports streaming responses
57
    from OpenAI API. It uses [ChatMessage](https://docs.haystack.deepset.ai/docs/chatmessage)
58
    format in input and output.
59

60
    You can customize how the text is generated by passing parameters to the
61
    OpenAI API. Use the `**generation_kwargs` argument when you initialize
62
    the component or when you run it. Any parameter that works with
63
    `openai.ChatCompletion.create` will work here too.
64

65
    For details on OpenAI API parameters, see
66
    [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat).
67

68
    ### Usage example
69

70
    ```python
71
    from haystack.components.generators.chat import OpenAIChatGenerator
72
    from haystack.dataclasses import ChatMessage
73

74
    messages = [ChatMessage.from_user("What's Natural Language Processing?")]
75

76
    client = OpenAIChatGenerator()
77
    response = client.run(messages)
78
    print(response)
79
    ```
80
    Output:
81
    ```
82
    {'replies':
83
        [ChatMessage(_role=<ChatRole.ASSISTANT: 'assistant'>, _content=
84
        [TextContent(text="Natural Language Processing (NLP) is a branch of artificial intelligence
85
            that focuses on enabling computers to understand, interpret, and generate human language in
86
            a way that is meaningful and useful.")],
87
         _name=None,
88
         _meta={'model': 'gpt-4o-mini', 'index': 0, 'finish_reason': 'stop',
89
         'usage': {'prompt_tokens': 15, 'completion_tokens': 36, 'total_tokens': 51}})
90
        ]
91
    }
92
    ```
93
    """
94

95
    def __init__(  # pylint: disable=too-many-positional-arguments
1✔
96
        self,
97
        api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"),
98
        model: str = "gpt-4o-mini",
99
        streaming_callback: Optional[StreamingCallbackT] = None,
100
        api_base_url: Optional[str] = None,
101
        organization: Optional[str] = None,
102
        generation_kwargs: Optional[dict[str, Any]] = None,
103
        timeout: Optional[float] = None,
104
        max_retries: Optional[int] = None,
105
        tools: Optional[Union[list[Tool], Toolset]] = None,
106
        tools_strict: bool = False,
107
        http_client_kwargs: Optional[dict[str, Any]] = None,
108
    ):
109
        """
110
        Creates an instance of OpenAIChatGenerator. Unless specified otherwise in `model`, uses OpenAI's gpt-4o-mini
111

112
        Before initializing the component, you can set the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES'
113
        environment variables to override the `timeout` and `max_retries` parameters respectively
114
        in the OpenAI client.
115

116
        :param api_key: The OpenAI API key.
117
            You can set it with an environment variable `OPENAI_API_KEY`, or pass with this parameter
118
            during initialization.
119
        :param model: The name of the model to use.
120
        :param streaming_callback: A callback function that is called when a new token is received from the stream.
121
            The callback function accepts [StreamingChunk](https://docs.haystack.deepset.ai/docs/data-classes#streamingchunk)
122
            as an argument.
123
        :param api_base_url: An optional base URL.
124
        :param organization: Your organization ID, defaults to `None`. See
125
        [production best practices](https://platform.openai.com/docs/guides/production-best-practices/setting-up-your-organization).
126
        :param generation_kwargs: Other parameters to use for the model. These parameters are sent directly to
127
            the OpenAI endpoint. See OpenAI [documentation](https://platform.openai.com/docs/api-reference/chat) for
128
            more details.
129
            Some of the supported parameters:
130
            - `max_completion_tokens`: An upper bound for the number of tokens that can be generated for a completion,
131
                including visible output tokens and reasoning tokens.
132
            - `temperature`: What sampling temperature to use. Higher values mean the model will take more risks.
133
                Try 0.9 for more creative applications and 0 (argmax sampling) for ones with a well-defined answer.
134
            - `top_p`: An alternative to sampling with temperature, called nucleus sampling, where the model
135
                considers the results of the tokens with top_p probability mass. For example, 0.1 means only the tokens
136
                comprising the top 10% probability mass are considered.
137
            - `n`: How many completions to generate for each prompt. For example, if the LLM gets 3 prompts and n is 2,
138
                it will generate two completions for each of the three prompts, ending up with 6 completions in total.
139
            - `stop`: One or more sequences after which the LLM should stop generating tokens.
140
            - `presence_penalty`: What penalty to apply if a token is already present at all. Bigger values mean
141
                the model will be less likely to repeat the same token in the text.
142
            - `frequency_penalty`: What penalty to apply if a token has already been generated in the text.
143
                Bigger values mean the model will be less likely to repeat the same token in the text.
144
            - `logit_bias`: Add a logit bias to specific tokens. The keys of the dictionary are tokens, and the
145
                values are the bias to add to that token.
146
            - `response_format`: A JSON schema or a Pydantic model that enforces the structure of the model's response.
147
                If provided, the output will always be validated against this
148
                format (unless the model returns a tool call).
149
                For details, see the [OpenAI Structured Outputs documentation](https://platform.openai.com/docs/guides/structured-outputs).
150
                Notes:
151
                - This parameter accepts Pydantic models and JSON schemas for latest models starting from GPT-4o.
152
                  Older models only support basic version of structured outputs through `{"type": "json_object"}`.
153
                  For detailed information on JSON mode, see the [OpenAI Structured Outputs documentation](https://platform.openai.com/docs/guides/structured-outputs#json-mode).
154
                - For structured outputs with streaming,
155
                  the `response_format` must be a JSON schema and not a Pydantic model.
156
        :param timeout:
157
            Timeout for OpenAI client calls. If not set, it defaults to either the
158
            `OPENAI_TIMEOUT` environment variable, or 30 seconds.
159
        :param max_retries:
160
            Maximum number of retries to contact OpenAI after an internal error.
161
            If not set, it defaults to either the `OPENAI_MAX_RETRIES` environment variable, or set to 5.
162
        :param tools:
163
            A list of tools or a Toolset for which the model can prepare calls. This parameter can accept either a
164
            list of `Tool` objects or a `Toolset` instance.
165
        :param tools_strict:
166
            Whether to enable strict schema adherence for tool calls. If set to `True`, the model will follow exactly
167
            the schema provided in the `parameters` field of the tool definition, but this may increase latency.
168
        :param http_client_kwargs:
169
            A dictionary of keyword arguments to configure a custom `httpx.Client`or `httpx.AsyncClient`.
170
            For more information, see the [HTTPX documentation](https://www.python-httpx.org/api/#client).
171

172
        """
173
        self.api_key = api_key
1✔
174
        self.model = model
1✔
175
        self.generation_kwargs = generation_kwargs or {}
1✔
176
        self.streaming_callback = streaming_callback
1✔
177
        self.api_base_url = api_base_url
1✔
178
        self.organization = organization
1✔
179
        self.timeout = timeout
1✔
180
        self.max_retries = max_retries
1✔
181
        self.tools = tools  # Store tools as-is, whether it's a list or a Toolset
1✔
182
        self.tools_strict = tools_strict
1✔
183
        self.http_client_kwargs = http_client_kwargs
1✔
184
        # Check for duplicate tool names
185
        _check_duplicate_tool_names(list(self.tools or []))
1✔
186

187
        if timeout is None:
1✔
188
            timeout = float(os.environ.get("OPENAI_TIMEOUT", "30.0"))
1✔
189
        if max_retries is None:
1✔
190
            max_retries = int(os.environ.get("OPENAI_MAX_RETRIES", "5"))
1✔
191

192
        client_kwargs: dict[str, Any] = {
1✔
193
            "api_key": api_key.resolve_value(),
194
            "organization": organization,
195
            "base_url": api_base_url,
196
            "timeout": timeout,
197
            "max_retries": max_retries,
198
        }
199

200
        self.client = OpenAI(http_client=init_http_client(self.http_client_kwargs, async_client=False), **client_kwargs)
1✔
201
        self.async_client = AsyncOpenAI(
1✔
202
            http_client=init_http_client(self.http_client_kwargs, async_client=True), **client_kwargs
203
        )
204

205
    def _get_telemetry_data(self) -> dict[str, Any]:
1✔
206
        """
207
        Data that is sent to Posthog for usage analytics.
208
        """
209
        return {"model": self.model}
1✔
210

211
    def to_dict(self) -> dict[str, Any]:
1✔
212
        """
213
        Serialize this component to a dictionary.
214

215
        :returns:
216
            The serialized component as a dictionary.
217
        """
218
        callback_name = serialize_callable(self.streaming_callback) if self.streaming_callback else None
1✔
219
        generation_kwargs = self.generation_kwargs.copy()
1✔
220
        response_format = generation_kwargs.get("response_format")
1✔
221

222
        # If the response format is a Pydantic model, it's converted to openai's json schema format
223
        # If it's already a json schema, it's left as is
224
        if response_format and isinstance(response_format, type) and issubclass(response_format, BaseModel):
1✔
225
            json_schema = {
1✔
226
                "type": "json_schema",
227
                "json_schema": {
228
                    "name": response_format.__name__,
229
                    "strict": True,
230
                    "schema": to_strict_json_schema(response_format),
231
                },
232
            }
233
            generation_kwargs["response_format"] = json_schema
1✔
234

235
        return default_to_dict(
1✔
236
            self,
237
            model=self.model,
238
            streaming_callback=callback_name,
239
            api_base_url=self.api_base_url,
240
            organization=self.organization,
241
            generation_kwargs=generation_kwargs,
242
            api_key=self.api_key.to_dict(),
243
            timeout=self.timeout,
244
            max_retries=self.max_retries,
245
            tools=serialize_tools_or_toolset(self.tools),
246
            tools_strict=self.tools_strict,
247
            http_client_kwargs=self.http_client_kwargs,
248
        )
249

250
    @classmethod
1✔
251
    def from_dict(cls, data: dict[str, Any]) -> "OpenAIChatGenerator":
1✔
252
        """
253
        Deserialize this component from a dictionary.
254

255
        :param data: The dictionary representation of this component.
256
        :returns:
257
            The deserialized component instance.
258
        """
259
        deserialize_secrets_inplace(data["init_parameters"], keys=["api_key"])
1✔
260
        deserialize_tools_or_toolset_inplace(data["init_parameters"], key="tools")
1✔
261
        init_params = data.get("init_parameters", {})
1✔
262
        serialized_callback_handler = init_params.get("streaming_callback")
1✔
263

264
        if serialized_callback_handler:
1✔
265
            data["init_parameters"]["streaming_callback"] = deserialize_callable(serialized_callback_handler)
1✔
266
        return default_from_dict(cls, data)
1✔
267

268
    @component.output_types(replies=list[ChatMessage])
1✔
269
    def run(
1✔
270
        self,
271
        messages: list[ChatMessage],
272
        streaming_callback: Optional[StreamingCallbackT] = None,
273
        generation_kwargs: Optional[dict[str, Any]] = None,
274
        *,
275
        tools: Optional[Union[list[Tool], Toolset]] = None,
276
        tools_strict: Optional[bool] = None,
277
    ):
278
        """
279
        Invokes chat completion based on the provided messages and generation parameters.
280

281
        :param messages:
282
            A list of ChatMessage instances representing the input messages.
283
        :param streaming_callback:
284
            A callback function that is called when a new token is received from the stream.
285
        :param generation_kwargs:
286
            Additional keyword arguments for text generation. These parameters will
287
            override the parameters passed during component initialization.
288
            For details on OpenAI API parameters, see [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat/create).
289
        :param tools:
290
            A list of tools or a Toolset for which the model can prepare calls. If set, it will override the
291
            `tools` parameter set during component initialization. This parameter can accept either a list of
292
            `Tool` objects or a `Toolset` instance.
293
        :param tools_strict:
294
            Whether to enable strict schema adherence for tool calls. If set to `True`, the model will follow exactly
295
            the schema provided in the `parameters` field of the tool definition, but this may increase latency.
296
            If set, it will override the `tools_strict` parameter set during component initialization.
297

298
        :returns:
299
            A dictionary with the following key:
300
            - `replies`: A list containing the generated responses as ChatMessage instances.
301
        """
302
        if len(messages) == 0:
1✔
303
            return {"replies": []}
1✔
304

305
        streaming_callback = select_streaming_callback(
1✔
306
            init_callback=self.streaming_callback, runtime_callback=streaming_callback, requires_async=False
307
        )
308
        chat_completion: Union[Stream[ChatCompletionChunk], ChatCompletion, ParsedChatCompletion]
309

310
        api_args = self._prepare_api_call(
1✔
311
            messages=messages,
312
            streaming_callback=streaming_callback,
313
            generation_kwargs=generation_kwargs,
314
            tools=tools,
315
            tools_strict=tools_strict,
316
        )
317
        openai_endpoint = api_args.pop("openai_endpoint")
1✔
318
        openai_endpoint_method = getattr(self.client.chat.completions, openai_endpoint)
1✔
319
        chat_completion = openai_endpoint_method(**api_args)
1✔
320

321
        if streaming_callback is not None:
1✔
322
            completions = self._handle_stream_response(
1✔
323
                # we cannot check isinstance(chat_completion, Stream) because some observability tools wrap Stream
324
                # and return a different type. See https://github.com/deepset-ai/haystack/issues/9014.
325
                chat_completion,  # type: ignore
326
                streaming_callback,
327
            )
328

329
        else:
330
            assert isinstance(chat_completion, ChatCompletion), "Unexpected response type for non-streaming request."
1✔
331
            completions = [
1✔
332
                _convert_chat_completion_to_chat_message(chat_completion, choice) for choice in chat_completion.choices
333
            ]
334

335
        # before returning, do post-processing of the completions
336
        for message in completions:
1✔
337
            _check_finish_reason(message.meta)
1✔
338

339
        return {"replies": completions}
1✔
340

341
    @component.output_types(replies=list[ChatMessage])
1✔
342
    async def run_async(
1✔
343
        self,
344
        messages: list[ChatMessage],
345
        streaming_callback: Optional[StreamingCallbackT] = None,
346
        generation_kwargs: Optional[dict[str, Any]] = None,
347
        *,
348
        tools: Optional[Union[list[Tool], Toolset]] = None,
349
        tools_strict: Optional[bool] = None,
350
    ):
351
        """
352
        Asynchronously invokes chat completion based on the provided messages and generation parameters.
353

354
        This is the asynchronous version of the `run` method. It has the same parameters and return values
355
        but can be used with `await` in async code.
356

357
        :param messages:
358
            A list of ChatMessage instances representing the input messages.
359
        :param streaming_callback:
360
            A callback function that is called when a new token is received from the stream.
361
            Must be a coroutine.
362
        :param generation_kwargs:
363
            Additional keyword arguments for text generation. These parameters will
364
            override the parameters passed during component initialization.
365
            For details on OpenAI API parameters, see [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat/create).
366
        :param tools:
367
            A list of tools or a Toolset for which the model can prepare calls. If set, it will override the
368
            `tools` parameter set during component initialization. This parameter can accept either a list of
369
            `Tool` objects or a `Toolset` instance.
370
        :param tools_strict:
371
            Whether to enable strict schema adherence for tool calls. If set to `True`, the model will follow exactly
372
            the schema provided in the `parameters` field of the tool definition, but this may increase latency.
373
            If set, it will override the `tools_strict` parameter set during component initialization.
374

375
        :returns:
376
            A dictionary with the following key:
377
            - `replies`: A list containing the generated responses as ChatMessage instances.
378
        """
379
        # validate and select the streaming callback
380
        streaming_callback = select_streaming_callback(
1✔
381
            init_callback=self.streaming_callback, runtime_callback=streaming_callback, requires_async=True
382
        )
383
        chat_completion: Union[AsyncStream[ChatCompletionChunk], ChatCompletion, ParsedChatCompletion]
384

385
        if len(messages) == 0:
1✔
386
            return {"replies": []}
×
387

388
        api_args = self._prepare_api_call(
1✔
389
            messages=messages,
390
            streaming_callback=streaming_callback,
391
            generation_kwargs=generation_kwargs,
392
            tools=tools,
393
            tools_strict=tools_strict,
394
        )
395

396
        openai_endpoint = api_args.pop("openai_endpoint")
1✔
397
        openai_endpoint_method = getattr(self.async_client.chat.completions, openai_endpoint)
1✔
398
        chat_completion = await openai_endpoint_method(**api_args)
1✔
399

400
        if streaming_callback is not None:
1✔
401
            completions = await self._handle_async_stream_response(
1✔
402
                # we cannot check isinstance(chat_completion, AsyncStream) because some observability tools wrap
403
                # AsyncStream and return a different type. See https://github.com/deepset-ai/haystack/issues/9014.
404
                chat_completion,  # type: ignore
405
                streaming_callback,
406
            )
407

408
        else:
409
            assert isinstance(chat_completion, ChatCompletion), "Unexpected response type for non-streaming request."
1✔
410
            completions = [
1✔
411
                _convert_chat_completion_to_chat_message(chat_completion, choice) for choice in chat_completion.choices
412
            ]
413

414
        # before returning, do post-processing of the completions
415
        for message in completions:
1✔
416
            _check_finish_reason(message.meta)
1✔
417

418
        return {"replies": completions}
1✔
419

420
    def _prepare_api_call(  # noqa: PLR0913
1✔
421
        self,
422
        *,
423
        messages: list[ChatMessage],
424
        streaming_callback: Optional[StreamingCallbackT] = None,
425
        generation_kwargs: Optional[dict[str, Any]] = None,
426
        tools: Optional[Union[list[Tool], Toolset]] = None,
427
        tools_strict: Optional[bool] = None,
428
    ) -> dict[str, Any]:
429
        # update generation kwargs by merging with the generation kwargs passed to the run method
430
        generation_kwargs = {**self.generation_kwargs, **(generation_kwargs or {})}
1✔
431

432
        is_streaming = streaming_callback is not None
1✔
433
        num_responses = generation_kwargs.pop("n", 1)
1✔
434

435
        if is_streaming and num_responses > 1:
1✔
436
            raise ValueError("Cannot stream multiple responses, please set n=1.")
×
437
        response_format = generation_kwargs.pop("response_format", None)
1✔
438

439
        # adapt ChatMessage(s) to the format expected by the OpenAI API
440
        openai_formatted_messages = [message.to_openai_dict_format() for message in messages]
1✔
441

442
        tools = tools or self.tools
1✔
443
        if isinstance(tools, Toolset):
1✔
444
            tools = list(tools)
×
445
        tools_strict = tools_strict if tools_strict is not None else self.tools_strict
1✔
446
        _check_duplicate_tool_names(tools)
1✔
447

448
        openai_tools = {}
1✔
449
        if tools:
1✔
450
            tool_definitions = []
1✔
451
            for t in tools:
1✔
452
                function_spec = {**t.tool_spec}
1✔
453
                if tools_strict:
1✔
454
                    function_spec["strict"] = True
1✔
455
                    function_spec["parameters"]["additionalProperties"] = False
1✔
456
                tool_definitions.append({"type": "function", "function": function_spec})
1✔
457
            openai_tools = {"tools": tool_definitions}
1✔
458

459
        base_args = {
1✔
460
            "model": self.model,
461
            "messages": openai_formatted_messages,
462
            "n": num_responses,
463
            **openai_tools,
464
            **generation_kwargs,
465
        }
466

467
        if response_format and not is_streaming:
1✔
468
            # for structured outputs without streaming, we use openai's parse endpoint
469
            # Note: `stream` cannot be passed to chat.completions.parse
470
            # we pass a key `openai_endpoint` as a hint to the run method to use the parse endpoint
471
            # this key will be removed before the API call is made
472
            return {**base_args, "response_format": response_format, "openai_endpoint": "parse"}
1✔
473

474
        # for structured outputs with streaming, we use openai's create endpoint
475
        # we pass a key `openai_endpoint` as a hint to the run method to use the create endpoint
476
        # this key will be removed before the API call is made
477
        final_args = {**base_args, "stream": is_streaming, "openai_endpoint": "create"}
1✔
478

479
        # We only set the response_format parameter if it's not None since None is not a valid value in the API.
480
        if response_format:
1✔
481
            final_args["response_format"] = response_format
1✔
482
        return final_args
1✔
483

484
    def _handle_stream_response(self, chat_completion: Stream, callback: SyncStreamingCallbackT) -> list[ChatMessage]:
1✔
485
        component_info = ComponentInfo.from_component(self)
1✔
486
        chunks: list[StreamingChunk] = []
1✔
487
        for chunk in chat_completion:  # pylint: disable=not-an-iterable
1✔
488
            assert len(chunk.choices) <= 1, "Streaming responses should have at most one choice."
1✔
489
            chunk_delta = _convert_chat_completion_chunk_to_streaming_chunk(
1✔
490
                chunk=chunk, previous_chunks=chunks, component_info=component_info
491
            )
492
            chunks.append(chunk_delta)
1✔
493
            callback(chunk_delta)
1✔
494
        return [_convert_streaming_chunks_to_chat_message(chunks=chunks)]
1✔
495

496
    async def _handle_async_stream_response(
1✔
497
        self, chat_completion: AsyncStream, callback: AsyncStreamingCallbackT
498
    ) -> list[ChatMessage]:
499
        component_info = ComponentInfo.from_component(self)
1✔
500
        chunks: list[StreamingChunk] = []
1✔
501
        async for chunk in chat_completion:  # pylint: disable=not-an-iterable
1✔
502
            assert len(chunk.choices) <= 1, "Streaming responses should have at most one choice."
1✔
503
            chunk_delta = _convert_chat_completion_chunk_to_streaming_chunk(
1✔
504
                chunk=chunk, previous_chunks=chunks, component_info=component_info
505
            )
506
            chunks.append(chunk_delta)
1✔
507
            await callback(chunk_delta)
1✔
508
        return [_convert_streaming_chunks_to_chat_message(chunks=chunks)]
1✔
509

510

511
def _check_finish_reason(meta: dict[str, Any]) -> None:
1✔
512
    if meta["finish_reason"] == "length":
1✔
513
        logger.warning(
1✔
514
            "The completion for index {index} has been truncated before reaching a natural stopping point. "
515
            "Increase the max_completion_tokens parameter to allow for longer completions.",
516
            index=meta["index"],
517
            finish_reason=meta["finish_reason"],
518
        )
519
    if meta["finish_reason"] == "content_filter":
1✔
520
        logger.warning(
1✔
521
            "The completion for index {index} has been truncated due to the content filter.",
522
            index=meta["index"],
523
            finish_reason=meta["finish_reason"],
524
        )
525

526

527
def _convert_chat_completion_to_chat_message(
1✔
528
    completion: Union[ChatCompletion, ParsedChatCompletion], choice: Choice
529
) -> ChatMessage:
530
    """
531
    Converts the non-streaming response from the OpenAI API to a ChatMessage.
532

533
    :param completion: The completion returned by the OpenAI API.
534
    :param choice: The choice returned by the OpenAI API.
535
    :return: The ChatMessage.
536
    """
537
    message: Union[ChatCompletionMessage, ParsedChatCompletionMessage] = choice.message
1✔
538
    text = message.content
1✔
539
    tool_calls = []
1✔
540
    if message.tool_calls:
1✔
541
        # we currently only support function tools (not custom tools)
542
        # https://platform.openai.com/docs/guides/function-calling#custom-tools
543
        openai_tool_calls = [tc for tc in message.tool_calls if not isinstance(tc, ChatCompletionMessageCustomToolCall)]
1✔
544
        for openai_tc in openai_tool_calls:
1✔
545
            arguments_str = openai_tc.function.arguments
1✔
546
            try:
1✔
547
                arguments = json.loads(arguments_str)
1✔
548
                tool_calls.append(ToolCall(id=openai_tc.id, tool_name=openai_tc.function.name, arguments=arguments))
1✔
549
            except json.JSONDecodeError:
1✔
550
                logger.warning(
1✔
551
                    "OpenAI returned a malformed JSON string for tool call arguments. This tool call "
552
                    "will be skipped. To always generate a valid JSON, set `tools_strict` to `True`. "
553
                    "Tool call ID: {_id}, Tool name: {_name}, Arguments: {_arguments}",
554
                    _id=openai_tc.id,
555
                    _name=openai_tc.function.name,
556
                    _arguments=arguments_str,
557
                )
558

559
    chat_message = ChatMessage.from_assistant(
1✔
560
        text=text,
561
        tool_calls=tool_calls,
562
        meta={
563
            "model": completion.model,
564
            "index": choice.index,
565
            "finish_reason": choice.finish_reason,
566
            "usage": _serialize_usage(completion.usage),
567
        },
568
    )
569

570
    return chat_message
1✔
571

572

573
def _convert_chat_completion_chunk_to_streaming_chunk(
1✔
574
    chunk: ChatCompletionChunk, previous_chunks: list[StreamingChunk], component_info: Optional[ComponentInfo] = None
575
) -> StreamingChunk:
576
    """
577
    Converts the streaming response chunk from the OpenAI API to a StreamingChunk.
578

579
    :param chunk: The chunk returned by the OpenAI API.
580
    :param previous_chunks: A list of previously received StreamingChunks.
581
    :param component_info: An optional `ComponentInfo` object containing information about the component that
582
        generated the chunk, such as the component name and type.
583

584
    :returns:
585
        A StreamingChunk object representing the content of the chunk from the OpenAI API.
586
    """
587
    finish_reason_mapping: dict[str, FinishReason] = {
1✔
588
        "stop": "stop",
589
        "length": "length",
590
        "content_filter": "content_filter",
591
        "tool_calls": "tool_calls",
592
        "function_call": "tool_calls",
593
    }
594
    # On very first chunk so len(previous_chunks) == 0, the Choices field only provides role info (e.g. "assistant")
595
    # Choices is empty if include_usage is set to True where the usage information is returned.
596
    if len(chunk.choices) == 0:
1✔
597
        return StreamingChunk(
1✔
598
            content="",
599
            component_info=component_info,
600
            # Index is None since it's only set to an int when a content block is present
601
            index=None,
602
            finish_reason=None,
603
            meta={
604
                "model": chunk.model,
605
                "received_at": datetime.now().isoformat(),
606
                "usage": _serialize_usage(chunk.usage),
607
            },
608
        )
609

610
    choice: ChunkChoice = chunk.choices[0]
1✔
611

612
    # create a list of ToolCallDelta objects from the tool calls
613
    if choice.delta.tool_calls:
1✔
614
        tool_calls_deltas = []
1✔
615
        for tool_call in choice.delta.tool_calls:
1✔
616
            function = tool_call.function
1✔
617
            tool_calls_deltas.append(
1✔
618
                ToolCallDelta(
619
                    index=tool_call.index,
620
                    id=tool_call.id,
621
                    tool_name=function.name if function else None,
622
                    arguments=function.arguments if function and function.arguments else None,
623
                )
624
            )
625
        chunk_message = StreamingChunk(
1✔
626
            content=choice.delta.content or "",
627
            component_info=component_info,
628
            # We adopt the first tool_calls_deltas.index as the overall index of the chunk.
629
            index=tool_calls_deltas[0].index,
630
            tool_calls=tool_calls_deltas,
631
            start=tool_calls_deltas[0].tool_name is not None,
632
            finish_reason=finish_reason_mapping.get(choice.finish_reason) if choice.finish_reason else None,
633
            meta={
634
                "model": chunk.model,
635
                "index": choice.index,
636
                "tool_calls": choice.delta.tool_calls,
637
                "finish_reason": choice.finish_reason,
638
                "received_at": datetime.now().isoformat(),
639
                "usage": _serialize_usage(chunk.usage),
640
            },
641
        )
642
        return chunk_message
1✔
643

644
    # On very first chunk the choice field only provides role info (e.g. "assistant") so we set index to None
645
    # We set all chunks missing the content field to index of None. E.g. can happen if chunk only contains finish
646
    # reason.
647
    if choice.delta.content is None or choice.delta.role is not None:
1✔
648
        resolved_index = None
1✔
649
    else:
650
        # We set the index to be 0 since if text content is being streamed then no tool calls are being streamed
651
        # NOTE: We may need to revisit this if OpenAI allows planning/thinking content before tool calls like
652
        #       Anthropic Claude
653
        resolved_index = 0
1✔
654
    chunk_message = StreamingChunk(
1✔
655
        content=choice.delta.content or "",
656
        component_info=component_info,
657
        index=resolved_index,
658
        # The first chunk is always a start message chunk that only contains role information, so if we reach here
659
        # and previous_chunks is length 1 then this is the start of text content.
660
        start=len(previous_chunks) == 1,
661
        finish_reason=finish_reason_mapping.get(choice.finish_reason) if choice.finish_reason else None,
662
        meta={
663
            "model": chunk.model,
664
            "index": choice.index,
665
            "tool_calls": choice.delta.tool_calls,
666
            "finish_reason": choice.finish_reason,
667
            "received_at": datetime.now().isoformat(),
668
            "usage": _serialize_usage(chunk.usage),
669
        },
670
    )
671
    return chunk_message
1✔
672

673

674
def _serialize_usage(usage):
1✔
675
    """Convert OpenAI usage object to serializable dict recursively"""
676
    if hasattr(usage, "model_dump"):
1✔
677
        return usage.model_dump()
1✔
678
    elif hasattr(usage, "__dict__"):
1✔
679
        return {k: _serialize_usage(v) for k, v in usage.__dict__.items() if not k.startswith("_")}
×
680
    elif isinstance(usage, dict):
1✔
681
        return {k: _serialize_usage(v) for k, v in usage.items()}
×
682
    elif isinstance(usage, list):
1✔
683
        return [_serialize_usage(item) for item in usage]
×
684
    else:
685
        return usage
1✔
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