Skip to main content

Overview

PyPI version

The package provides helpers facilitating integration of DIAL API with Langchain library.

Passing DIAL-specific extra fields in Langchain requests/responses

Unlike openai library, langchain-openai library doesn't allow to pass extra request/response parameters to/from the upstream model.

The minimal example highlighting the issue could be found in the example folder:

cd example
python -m venv .venv
source .venv/bin/activate
pip install -q -r requirements.txt
python -m app
Received the following extra fields:
(1) ☑ request.extra_field
(2) ☑ request.tools[0].extra_field
(3) ☐ request.messages[0].extra_field
(4) ☐ response.message.extra_field
(5) ☐ response.extra_field

langchain-openai ignores certain extra fields, meaning that the upstream endpoint won't receive (2) and the client won't receive (4) and (5) if they were sent by the upstream.

Note that top-level request extra fields and tool definition extra fields do actually reach the upstream.

Solution

One way to fix the issue, is to modify the Langchain methods that ignore these extra fields so that they are taken into account.

This is achieved via monkey-patching certain private methods in langchain-openai that do the conversion from the Langchain datatypes to dictionaries and vice versa.

Usage

Install the aidial-integration-langchain package as a dependency in your project:

pip install aidial-integration-langchain

Then import aidial_integration_langchain before importing any Langchain module to apply the patches:

import aidial_integration_langchain.patch # isort:skip  # noqa: F401 # type: ignore

from langchain_openai import AzureChatOpenAI
...

The same example as above, but with the patch applied:

cd example
python -m venv .venv
source .venv/bin/activate
pip install -q -r requirements.txt
pip install -q aidial-integration-langchain
python -m app patch
Received the following extra fields:
(1) ☑ request.extra_field
(2) ☑ request.tools[0].extra_field
(3) ☑ request.messages[0].extra_field
(4) ☑ response.message.extra_field
(5) ☑ response.extra_field

Supported Langchain versions

The following langchain-openai versions have been tested for Python 3.9, 3.10, 3.11, 3.12, and 3.13:

Version Request per-message Response per-message Response top-level
>=0.1.1,<=0.1.22 🟢 🟢 🔴
>=0.1.23,<=0.1.25 🟢 🟢 🟢
>=0.2.0,<=0.2.14 🟢 🟢 🟢
>=0.3.0,<=0.3.18 🟢 🟢 🟢

[!NOTE] The patched langchain-openai<=0.1.22 doesn't support response top-level extra fields, since the structure of the code back then was not very amicable for monkey-patching in this particular respect.

Configuration

The list of extra fields that are allowed to pass-through is controlled by the following environment variables.

Name Default Description
LC_EXTRA_REQUEST_MESSAGE_FIELDS custom_content A comma-separated list of extra message fields allowed to pass-through in chat completion requests.
LC_EXTRA_RESPONSE_MESSAGE_FIELDS custom_content A comma-separated list of extra message fields allowed to pass-through in chat completion responses.
LC_EXTRA_RESPONSE_FIELDS statistics A comma-separated list of extra fields allowed to pass-through on the top-level of the chat completion responses.

Release files for aidial-integration-langchain 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aidial-integration-langchain 0.2.1
File Size Uploaded
aidial_integration_langchain-0.2.1.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aidial-integration-langchain 0.2.1
File Interpreter ABI Platform
aidial_integration_langchain-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / aidial_integration_langchain-0.2.1.tar.gz

Download URL aidial_integration_langchain-0.2.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
34bc0b84e32e90aaa6e39c07a46b0ebe742bfaf3a120c39ed716aa3a6fb117a9
BLAKE2b-256 checksum
How to use checksums
1fab6ad2018293c743a4db5d6ec9a7c1a1d239e7ee9b6c83c176de58fd950222
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.3

Release files / aidial_integration_langchain-0.2.1-py3-none-any.whl

Download URL aidial_integration_langchain-0.2.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aca85cbb5a207032815a4f3ab5afd305a5d276970d37db94a4bfd8fb336aeb35
BLAKE2b-256 checksum
How to use checksums
c3fc4c70bd6737b3e552446584f008dfb4f11b401a8d94eca6bb1a51ae54eb9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.3
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page