Commit Graph

71 Commits

Author SHA1 Message Date
github-actions[bot] a39f132b91 Update versions to 1.19.0-SNAPSHOT and 1.19.0-beta29-SNAPSHOT 2026-07-17 13:42:56 +00:00
github-actions[bot] 66ad5ee6d5 Release versions 1.18.0 and 1.18.0-beta28 2026-07-17 12:34:45 +00:00
Mohamed AIT ABDERRAHMAN 3689a5f154
Add OpenAI Text-to-Speech (TTS) support (#4697)
## Summary

Adds **Text-to-Speech (TTS)** support to LangChain4j: a new
provider-agnostic `TextToSpeechModel` abstraction in `langchain4j-core`,
an OpenAI implementation backed by the [OpenAI Speech

API](https://platform.openai.com/docs/api-reference/audio/createSpeech),
and the HTTP-client plumbing needed to return binary (audio) response
bodies intact.

  ## Changes

### `langchain4j-core` — new abstraction (`dev.langchain4j.model.audio`,
`@Experimental`)
- **`TextToSpeechModel`** — interface mirroring the existing
`AudioTranscriptionModel`:
- `synthesize(String text)` — convenience method using the model's
default voice
    - `synthesize(TextToSpeechRequest request)`
    - `provider()` defaulting to `OTHER`
- **`TextToSpeechRequest`** — carries `text` (validated non-blank) and
optional `voice`
- **`TextToSpeechResponse`** — wraps the core `Audio` type;
`from(Audio)` factory

  ### `langchain4j-open-ai` — OpenAI implementation
- **`OpenAiTextToSpeechModel`** — configurable `modelName`, `voice`
(default `alloy`), `timeout`, `maxRetries` (default `2`),
request/response logging, custom `baseUrl`, and `httpClientBuilder`.
Enforces
  OpenAI's 4096-character input limit. `provider()` → `OPEN_AI`.
- **`OpenAiTextToSpeechModelName`** — `tts-1`, `tts-1-hd`,
`gpt-4o-mini-tts`, `gpt-4o-mini-tts-2025-12-15`
- **`OpenAiTextToSpeechRequest` / `OpenAiTextToSpeechResponse`** —
internal DTOs (`internal.audio.texttospeech`); the response is built
from the raw HTTP body (binary audio, not JSON)
- **`OpenAiTextToSpeechModelBuilderFactory`** — SPI factory for builder
customization
- **`OpenAiClient.textToSpeech(...)`** — `POST /audio/speech`, mapping
the raw bytes + `Content-Type` into the response

  ### HTTP client layer (shared) — binary response support
- **`SuccessfulHttpResponse`** — body is now stored as `byte[]`.
`body()` is preserved and returns a decoded `String` (using the charset
from the `Content-Type` header, UTF-8 fallback); adds `bodyBytes()` for
raw binary access and `contentType()`. The `body(String)` builder is
kept and a `body(byte[])` overload added — **additive, no public method
removed or changed**.
- **`JdkHttpClient`** — switched to `BodyHandlers.ofByteArray()`;
**`ApacheHttpClient` / `OkHttpClient`** read the body as raw bytes, so
binary payloads (audio) are no longer corrupted by text decoding.
- **`HttpResponseLogger`** — no longer decodes/dumps binary bodies as
text; logs `[binary body, N bytes, content-type: ...]` instead.

  ### Tests
  - Core: `TextToSpeechModelTest`, `TextToSpeechRequestTest`
- HTTP: `SuccessfulHttpResponseTest` (charset decoding/edge cases),
`HttpClientIT` binary-response test asserting a valid MP3 is returned
across all client implementations
- OpenAI: `OpenAiTextToSpeechModelTest` (input-length and required-field
validation), `OpenAiTextToSpeechModelIT` (parameterized over every
supported model)

### Docs & API compatibility
- Added a **"Creating `OpenAiTextToSpeechModel`"** section to the OpenAI
docs page
- `revapi.json` updated to acknowledge the core
`TextToSpeechRequest`/`TextToSpeechResponse` types intentionally exposed
by `OpenAiTextToSpeechModel`

  ## Design notes
- Named `TextToSpeechModel` / `synthesize(...)` for discoverability (TTS
is the universally recognized term) and to read as a clear inverse of
the existing `AudioTranscriptionModel` / `transcribe(...)`.
- The shared HTTP-client change is **behavior-preserving**: for every
client (JDK / Apache / OkHttp), responses without an explicit charset
were already decoded as UTF-8, and explicit charsets are still
honored — verified empirically against `httpclient5 5.6.1` / `httpcore5
5.4`. The `String → byte[]` move is additive at the public API level.

  ## General checklist
- [x] There are no breaking changes (API, behaviour) — the shared
`SuccessfulHttpResponse` change is additive and verified
behavior-preserving; please review the HTTP-client layer regardless, as
it affects all
  providers
  - [x] I have added unit and/or integration tests for my change
  - [x] The tests cover both positive and negative cases
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [x] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and

[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
- [x] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)

---------

Co-authored-by: Dmytro Liubarskyi <ljubarskij@gmail.com>
2026-06-29 17:58:42 +02:00
github-actions[bot] 7d7c3349d7 Update versions to 1.18.0-SNAPSHOT and 1.18.0-beta28-SNAPSHOT 2026-06-26 14:49:40 +00:00
github-actions[bot] 207407aec9 Release versions 1.17.0 and 1.17.0-beta27 2026-06-26 13:13:06 +00:00
AceFun 152bce2b3e
test: add HTTP response verification for Ollama thinking mode (#5389)
Extend SpyingHttpClient to capture HTTP responses and SSE events, then
use it to verify raw response content in Ollama thinking mode tests,
completing 4 TODOs.

Changes to SpyingHttpClient:
- Add responses list to capture SuccessfulHttpResponse from sync calls
- Add sseEvents list to capture SSE event data from streaming calls
- Wrap ServerSentEventListener to intercept events before delegation

Changes to Ollama tests:
- OllamaChatModelThinkingIT: add SpyingHttpClient to 2 tests and verify
raw HTTP response contains/does not contain 'thinking' field
- OllamaStreamingChatModelThinkingIT: add SpyingHttpClient to 2 tests
and verify raw SSE events contain/does not contain 'thinking' field

---

## Issue
Closes # (discovered via code audit)

## Change

Extend `SpyingHttpClient` to capture HTTP responses and SSE events, then
use it to verify raw response content in Ollama thinking mode tests.

**SpyingHttpClient enhancements:**
- Add `responses` list to capture `SuccessfulHttpResponse` from sync
calls
- Add `sseEvents` list to capture SSE event data from streaming calls
- Wrap `ServerSentEventListener` to intercept events before delegation

**Test changes:**
- `OllamaChatModelThinkingIT`: add `SpyingHttpClient` to 2 tests and
verify raw HTTP response contains/does not contain "thinking" field
- `OllamaStreamingChatModelThinkingIT`: add `SpyingHttpClient` to 2
tests and verify raw SSE events contain/does not contain "thinking"
field

**Completed TODOs:**
- `OllamaChatModelThinkingIT.java:102` — verify raw HTTP response
contains "thinking" field
- `OllamaChatModelThinkingIT.java:133` — verify raw HTTP response does
not contain "thinking" field
- `OllamaStreamingChatModelThinkingIT.java:130` — verify raw SSE events
contain "thinking" field
- `OllamaStreamingChatModelThinkingIT.java:169` — verify raw SSE events
do not contain "thinking" field

## General checklist
- [X] There are no breaking changes (API, behaviour)
- [X] I have added unit and/or integration tests for my change
- [X] The tests cover both positive and negative cases
- [ ] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green — *requires Ollama
running locally*
- [ ] I have added/updated the documentation
2026-06-25 12:58:47 +02:00
github-actions[bot] 01de41d641 Update versions to 1.17.0-SNAPSHOT and 1.17.0-beta27-SNAPSHOT 2026-06-06 06:46:38 +00:00
github-actions[bot] cd836845dd Release versions 1.16.0 and 1.16.0-beta26 2026-06-05 15:46:56 +00:00
github-actions[bot] 6185599e37 Update versions to 1.16.0-SNAPSHOT and 1.16.0-beta26-SNAPSHOT 2026-05-15 16:21:14 +00:00
github-actions[bot] d0e54aa006 Release versions 1.15.0 and 1.15.0-beta25 2026-05-15 15:55:12 +00:00
github-actions[bot] 628ac34c01 Update versions to 1.15.0-SNAPSHOT and 1.15.0-beta25-SNAPSHOT 2026-04-30 18:43:12 +00:00
github-actions[bot] 4917afa297 Release versions 1.14.0 and 1.14.0-beta24 2026-04-30 18:10:30 +00:00
github-actions[bot] 4798a89d66 Update versions to 1.14.0-SNAPSHOT and 1.14.0-beta24-SNAPSHOT 2026-04-09 14:41:27 +00:00
github-actions[bot] 759cd9a236 Release versions 1.13.0 and 1.13.0-beta23 2026-04-09 13:07:30 +00:00
Dmytro Liubarskyi 423d2fc27c
OkHttpClient implementation (#4878)
## Change
Implemented `OkHttpClient` that can be used in Android projects.

## General checklist
- [X] There are no breaking changes (API, behaviour)
- [X] I have added unit and/or integration tests for my change
- [X] The tests cover both positive and negative cases
- [X] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [X] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
- [X] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)


## Checklist for adding new maven module
- [X] I have added my new module in the root `pom.xml` and
`langchain4j-bom/pom.xml`
2026-04-08 21:38:01 +02:00
Dmytro Liubarskyi e10abf04d0
Update versions to 1.13.0-SNAPSHOT and 1.13.0-beta23-SNAPSHOT (#4710)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-03-13 11:39:50 +01:00
Dmytro Liubarskyi c92ea033e4
Update versions to 1.13.0-SNAPSHOT and 1.13.0-beta22-SNAPSHOT (#4666)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-03-05 17:18:22 +01:00
Dmytro Liubarskyi 336b2accce
Update versions to 1.12.0-SNAPSHOT and 1.12.0-beta20-SNAPSHOT (#4537)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-02-04 14:27:44 +01:00
Dmytro Liubarskyi 0ca1a770b7 fixing flaky tests 2026-02-04 09:52:16 +01:00
Dmytro Liubarskyi d34b32bbbf fixing flaky tests 2026-02-03 19:22:57 +01:00
Dmytro Liubarskyi a97d508ae0 Add Apache HttpClient5 implementation for HTTP client (#4410) 2026-02-03 15:44:07 +01:00
Rui Yang f368ec0385
Add Apache HttpClient5 implementation for HTTP client (#4410)
<!--
Thank you so much for your contribution!

Please fill in all the sections below.
Please open the PR as a draft initially. Once it is reviewed and
approved, we will ask you to add documentation and examples.
Please note that PRs with breaking changes or without tests will be
rejected.

Please note that PRs will be reviewed based on the priority of the
issues they address.
We ask for your patience. We are doing our best to review your PR as
quickly as possible.
Please refrain from pinging and asking when it will be reviewed. Thank
you for understanding!
-->

## Issue
<!-- Please specify the ID of the issue this PR is addressing. For
example: "Closes #1234" or "Fixes #1234" -->
Closes #3775

## Change
<!-- Please describe the changes you made. -->
Simply integrated Apache HttpClient 5 and modified the timeout in the
HttpClientTimeoutIT class within the langchain4j-http-client test
methods to 1 second. Since Apache HttpClient 5's asynchronous timeout
cannot accurately implement a 250-millisecond timeout, the effect can
only be achieved by increasing the timeout duration.

## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes (API, behaviour)
- [x] I have added unit and/or integration tests for my change
- [x] The tests cover both positive and negative cases
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)


## Checklist for adding new maven module
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] I have added my new module in the root `pom.xml` and
`langchain4j-bom/pom.xml`


## Checklist for adding new embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends
from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT`
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreRemovalIT` that
extends from `EmbeddingStoreWithRemovalIT`

## Checklist for changing existing embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have manually verified that the
`{NameOfIntegration}EmbeddingStore` works correctly with the data
persisted using the latest released version of LangChain4j

---------

Co-authored-by: Dmytro Liubarskyi <ljubarskij@gmail.com>
2026-02-03 15:40:36 +01:00
Georgios Andrianakis 38d33ecabd
Introduce `langchain4j.http.clientBuilderFactory` system property (#4397)
This is done to allow users to select a specific implementation of
`HttpClientBuilderFactory` when there are multiple ones on the classpath

Co-authored-by: Dmytro Liubarskyi <ljubarskij@gmail.com>
2026-01-09 11:47:53 +01:00
Dmytro Liubarskyi 9017fb864a fixing flaky tests 2025-12-25 10:53:58 +01:00
Dmytro Liubarskyi 778be1b360
Update versions to 1.11.0-SNAPSHOT and 1.11.0-beta19-SNAPSHOT (#4285)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-12-24 15:38:05 +01:00
Dmytro Liubarskyi 8601de36aa Add OpenAI Transcription support (#4101) 2025-12-24 13:29:24 +01:00
Dmytro Liubarskyi e812116b9f Add OpenAI Transcription support (#4101) 2025-12-23 15:29:16 +01:00
Mohamed AIT ABDERRAHMAN d79209b414
Add OpenAI Transcription support (#4101)
<!--
Thank you so much for your contribution!

Please fill in all the sections below.
Please open the PR as a draft initially. Once it is reviewed and
approved, we will ask you to add documentation and examples.
Please note that PRs with breaking changes or without tests will be
rejected.

Please note that PRs will be reviewed based on the priority of the
issues they address.
We ask for your patience. We are doing our best to review your PR as
quickly as possible.
Please refrain from pinging and asking when it will be reviewed. Thank
you for understanding!
-->

## Issue
<!-- Please specify the ID of the issue this PR is addressing. For
example: "Closes #1234" or "Fixes #1234" -->
Closes #2985

## Change

This PR adds OpenAI audio transcription support to langchain4j,
implementing the `/audio/transcriptions` API endpoint. The
implementation follows the existing patterns in the codebase and
integrates cleanly with the HTTP client infrastructure.

### Key Changes
- Added `AudioTranscriptionModel` interface in langchain4j-core with
convenience methods
- Implemented `OpenAiAudioModel` with support for Whisper and GPT-4o
transcription models
- Added multipart/form-data support to HTTP client infrastructure
(`addFile()` method)
- Created `MultipartBodyPublisher` for JDK HTTP client to handle file
uploads
- Implemented audio format detection and binary/base64 data handling
- Added comprehensive test coverage including integration tests
- Updated Azure OpenAI implementation to use new interface

### Sequence Diagram

```mermaid
sequenceDiagram
    participant User
    participant OpenAiAudioModel
    participant OpenAiClient
    participant DefaultOpenAiClient
    participant JdkHttpClient
    participant MultipartBodyPublisher
    participant OpenAI API

    User->>OpenAiAudioModel: transcribe(AudioTranscriptionRequest)
    OpenAiAudioModel->>OpenAiAudioModel: validate(audioRequest)
    OpenAiAudioModel->>OpenAiAudioModel: requestBuilder()
    OpenAiAudioModel->>OpenAiClient: audioTranscription(OpenAiAudioTranscriptionRequest)
    OpenAiClient->>DefaultOpenAiClient: audioTranscription()
    DefaultOpenAiClient->>DefaultOpenAiClient: getBinaryDataFromAudio()
    DefaultOpenAiClient->>DefaultOpenAiClient: getAudioExtension()
    DefaultOpenAiClient->>DefaultOpenAiClient: build HttpRequest with multipart data
    DefaultOpenAiClient->>JdkHttpClient: execute(HttpRequest)
    JdkHttpClient->>JdkHttpClient: toJdkRequest()
    JdkHttpClient->>MultipartBodyPublisher: ofMultipartData()
    MultipartBodyPublisher->>MultipartBodyPublisher: addFormField()
    MultipartBodyPublisher->>MultipartBodyPublisher: addFile()
    MultipartBodyPublisher->>MultipartBodyPublisher: build()
    JdkHttpClient->>OpenAI API: POST /audio/transcriptions
    OpenAI API-->>JdkHttpClient: OpenAiAudioTranscriptionResponse
    JdkHttpClient-->>DefaultOpenAiClient: SuccessfulHttpResponse
    DefaultOpenAiClient-->>OpenAiClient: ParsedAndRawResponse
    OpenAiClient-->>OpenAiAudioModel: OpenAiAudioTranscriptionResponse
    OpenAiAudioModel->>OpenAiAudioModel: AudioTranscriptionResponse.from()
    OpenAiAudioModel-->>User: AudioTranscriptionResponse
```

## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes (API, behaviour)
- [x] I have added unit and/or integration tests for my change
- [ ] The tests cover both positive and negative cases
- [ ] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)


## Checklist for adding new maven module
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added my new module in the root `pom.xml` and
`langchain4j-bom/pom.xml`


## Checklist for adding new embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends
from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT`
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreRemovalIT` that
extends from `EmbeddingStoreWithRemovalIT`

## Checklist for changing existing embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have manually verified that the
`{NameOfIntegration}EmbeddingStore` works correctly with the data
persisted using the latest released version of LangChain4j

---------

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
2025-12-23 15:28:12 +01:00
Dmytro Liubarskyi 25d3b3ec87 cleaned up test dependencies 2025-12-10 10:18:30 +01:00
Dmytro Liubarskyi ca6097e35d
Update versions to 1.10.0-SNAPSHOT and 1.10.0-beta18-SNAPSHOT (#4152)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-11-28 12:21:30 +01:00
Dmytro Liubarskyi bc0801a4df
Update versions to 1.10.0-SNAPSHOT and 1.10.0-beta17-SNAPSHOT (#4140)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-11-26 17:38:36 +01:00
Dmytro Liubarskyi a473835133
Update versions to 1.9.0-SNAPSHOT and 1.9.0-beta16-SNAPSHOT (#3951)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-10-24 16:51:33 +02:00
Dmytro Liubarskyi ecc754ba1f
Streaming Cancellation (#3910)
## Issue
Closes https://github.com/langchain4j/langchain4j/issues/1146

## Change
Implemented streaming cancellation for the following APIs:
- `StreamingChatModel` + `StreamingChatResponseHandler`
- `TokenStream`

Implemented streaming cancellation for the following modules:
- Anthropic
- Azure OpenAI
- Bedrock
- Google AI Gemini
- Mistral
- Ollama
- OpenAI
- OpenAI Official
- Vertex AI Gemini

## Examples

### `StreamingChatModel` + `StreamingChatResponseHandler` APIs

If you wish to cancel the streaming, you can do so from one of the
following `StreamingChatResponseHandler` methods:
- `onPartialResponse(PartialResponse, PartialResponseContext)`
- `onPartialThinking(PartialThinking, PartialThinkingContext)`
- `onPartialToolCall(PartialToolCall, PartialToolCallContext)`

The context object contains the `StreamingHandle`, which can be used to
cancel the streaming:
```java
model.chat(userMessage, new StreamingChatResponseHandler() {

    @Override
    public void onPartialResponse(PartialResponse partialResponse, PartialResponseContext context) {
        process(partialResponse);
        if (shouldCancel()) {
            context.streamingHandle().cancel();
        }
    }

    @Override
    public void onCompleteResponse(ChatResponse completeResponse) {
        System.out.println("onCompleteResponse: " + completeResponse);
    }

    @Override
    public void onError(Throwable error) {
        error.printStackTrace();
    }
});
```

### `TokenStream` API

If you wish to cancel the streaming, you can do so from one of the
following callbacks:
- `onPartialResponseWithContext(BiConsumer<PartialResponse,
PartialResponseContext>)`
- `onPartialThinkingWithContext(BiConsumer<PartialThinking,
PartialThinkingContext>)`

For example:
```java
tokenStream
    .onPartialResponseWithContext((PartialResponse partialResponse, PartialResponseContext context) -> {
        process(partialResponse);
        if (shouldCancel()) {
            context.streamingHandle().cancel();
        }
    })
    .onCompleteResponse((ChatResponse response) -> futureResponse.complete(response))
    .onError((Throwable error) -> futureResponse.completeExceptionally(error))
    .start();
```

When `StreamingHandle.cancel()` is called, LangChain4j will close the
connection and stop the streaming.
Once `StreamingHandle.cancel()` has been called, `TokenStream` will not
receive any further callbacks.


## General checklist
- [x] There are no breaking changes (API, behaviour)
- [x] I have added unit and/or integration tests for my change
- [x] The tests cover both positive and negative cases
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [x] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
- [x] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring)
2025-10-23 20:00:57 +02:00
Mathias Geat 4fc9a5ecb7
feat(open-ai): Add support to set query parameters to an OpenAI request (#3880)
<!--
Thank you so much for your contribution!

Please fill in all the sections below.
Please open the PR as a draft initially. Once it is reviewed and
approved, we will ask you to add documentation and examples.
Please note that PRs with breaking changes or without tests will be
rejected.

Please note that PRs will be reviewed based on the priority of the
issues they address.
We ask for your patience. We are doing our best to review your PR as
quickly as possible.
Please refrain from pinging and asking when it will be reviewed. Thank
you for understanding!
-->

## Issue
<!-- Please specify the ID of the issue this PR is addressing. For
example: "Closes #1234" or "Fixes #1234" -->
Closes #3879 

## Change
<!-- Please describe the changes you made. -->

Adds a `customQueryParams` option to OpenAI requests (similar to
`customHeaders`) which allows setting query parameters to be included in
the URL when calling an OpenAI endpoint. As described in #3879, this can
be necessary when using the OpenAI language model with model providers
requiring certain query parameters (such as `?apiVersion=123`).

## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes (API, behaviour)
- [x] I have added unit and/or integration tests for my change
- [x] The tests cover both positive and negative cases
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)


## Checklist for adding new maven module
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added my new module in the root `pom.xml` and
`langchain4j-bom/pom.xml`


## Checklist for adding new embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends
from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT`
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreRemovalIT` that
extends from `EmbeddingStoreWithRemovalIT`

## Checklist for changing existing embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have manually verified that the
`{NameOfIntegration}EmbeddingStore` works correctly with the data
persisted using the latest released version of LangChain4j
2025-10-15 15:41:04 +02:00
Dmytro Liubarskyi c56332762d
OpenAiStreamingChatModel: expose raw HTTP response (#3828)
## Issue
Closes #3827

## Change


## General checklist
- [x] There are no breaking changes (API, behaviour)
- [x] I have added unit and/or integration tests for my change
- [ ] The tests cover both positive and negative cases
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
- [x] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)
2025-10-07 14:22:37 +02:00
Dmytro Liubarskyi 34632c06a2 nex dev iteration 2025-10-02 17:17:35 +02:00
Dmytro Liubarskyi 39ebd4120b Fixed https://github.com/langchain4j/langchain4j/issues/3776 2025-09-29 14:23:19 +02:00
Dmytro Liubarskyi 7add1a1b4e next dev iteration 2025-09-26 16:54:16 +02:00
Oleksandr Klymenko ced311b03d
test: Add validation and edge case test coverage for core request/response handling (#3727)
This PR adds critical test coverage for validation logic and edge case
scenarios across key request/response handling components:

**OpenAiUtilsTest (AiServicesModerationTest):**
- Added empty response choices validation to catch malformed API
responses
- Added null message content handling for graceful degradation
- Added function call validation with null arguments edge case

**ChatRequestTest (ChatResponseTest):**
- Added required field validation to prevent invalid request
construction
- Added empty messages array validation for comprehensive input checking
- Added parameter conflict detection to prevent inconsistent
configurations

**HttpRequestTest:**
- Added complete request building validation with all components
- Added URL construction testing with query parameters
- Added null body handling for GET requests and other scenarios
- Added null method validation to ensure required fields are set

These tests focus on preventing common configuration errors, handling
malformed inputs gracefully, and ensuring robust validation across the
request/response pipeline. They target the most likely failure scenarios
in production usage.

Signed-off-by: Oleksandr Klymenko <alexanderklmn@gmail.com>
2025-09-23 16:31:49 +02:00
Dmytro Liubarskyi afee79638b next dev iteration 2025-09-16 15:23:21 +02:00
Dmytro Liubarskyi dc233357ac fixed failing ITs 2025-08-29 10:04:21 +02:00
Dmytro Liubarskyi 7cc34306db next dev iteration 2025-08-29 08:35:38 +02:00
Loïc Mathieu 924ad084a0
Allow configuring the logger for logging requests and responses (#3561)
## Issue
Part-of #3556

Support for Google Gemini, Mistral and OpenAI providers.

## Change
Allow passing a `org.slf4j.Logger` to log requests and responses instead
of using the one provided by Langchain4J.
It allows user of the framework to use their own logger instead.

This is currently a quick prototype based on the discussions on #3556
The prototype only impact one provider, if it's agreed to pursue all
models would be impacted.

The solution is the more basic one, if we want, we can do something more
involved:
- Providing the logger using a `Producer<Logger>` instead of just a
`Logger`.
- Using the `java.util.logging.LogHandler` to have a more agnostic
logger
- Allow configuring also the level

The current prototype is only for openning discussions...

## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] There are no breaking changes
- [ ] I have added unit and/or integration tests for my change
- [ ] The tests cover both positive and negative cases
- [ ] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)


## Checklist for adding new maven module
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added my new module in the root `pom.xml` and
`langchain4j-bom/pom.xml`


## Checklist for adding new embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends
from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT`
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreRemovalIT` that
extends from `EmbeddingStoreWithRemovalIT`

## Checklist for changing existing embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have manually verified that the
`{NameOfIntegration}EmbeddingStore` works correctly with the data
persisted using the latest released version of LangChain4j

Co-authored-by: Dmytro Liubarskyi <ljubarskij@gmail.com>
2025-08-27 16:34:07 +02:00
◢ 徇 ◤ f60698986d
fix:[#3552] LoggingHttpClient should not use the DefaultServerSentEve… (#3554)
<!--
Thank you so much for your contribution!

Please fill in all the sections below.
Please open the PR as a draft initially. Once it is reviewed and
approved, we will ask you to add documentation and examples.
Please note that PRs with breaking changes or without tests will be
rejected.

Please note that PRs will be reviewed based on the priority of the
issues they address.
We ask for your patience. We are doing our best to review your PR as
quickly as possible.
Please refrain from pinging and asking when it will be reviewed. Thank
you for understanding!
-->

## Issue
Fixes #3552
## Change
<!-- Please describe the changes you made. -->
Override the execute method in LoggingHttpClient to use the
ServerSentEventParser from delegateHttpClient.

## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes
- [ ] I have added unit and/or integration tests for my change
- [ ] The tests cover both positive and negative cases
- [ ] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [ ] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring) (if
applicable)


## Checklist for adding new maven module
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added my new module in the root `pom.xml` and
`langchain4j-bom/pom.xml`


## Checklist for adding new embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends
from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT`
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreRemovalIT` that
extends from `EmbeddingStoreWithRemovalIT`

## Checklist for changing existing embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have manually verified that the
`{NameOfIntegration}EmbeddingStore` works correctly with the data
persisted using the latest released version of LangChain4j
2025-08-18 10:45:30 +02:00
Dmytro Liubarskyi 5b1b2e76d2 next dev iteration 2025-08-07 16:24:25 +02:00
Dmytro Liubarskyi 0a01b49951 next dev iteration 2025-07-29 17:50:28 +02:00
Tim te Beek ac3541c024
Apply AssertJ best practices as of July 2025 (#3398)
Hi! Hope these improvements are still appreciated; figured have you all
catch up to the latest version before enforcing that on PRs.

## Issue
- Continues https://github.com/langchain4j/langchain4j/pull/2561

## Change
Applied `mod run . --recipe AssertJ`, using
https://github.com/openrewrite/rewrite-testing-frameworks/releases/tag/v3.14.1

## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes
- [x] I have added unit and/or integration tests for my change
- [x] The tests cover both positive and negative cases
- [ ] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [x] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green

---------

Co-authored-by: Dmytro Liubarskyi <ljubarskij@gmail.com>
2025-07-25 14:26:36 +02:00
Dmytro Liubarskyi 03930ba850
Support thinking/reasoning (#3380)
## Issue
Closes https://github.com/langchain4j/langchain4j/issues/3013
Closes https://github.com/langchain4j/langchain4j/issues/2605
Closes https://github.com/langchain4j/langchain4j/issues/2472
Closes https://github.com/langchain4j/langchain4j/issues/3361
Closes https://github.com/langchain4j/langchain4j/issues/1224

## Change
Support thinking/reasoning feature in `ChatModel`/`StreamingChatModel`
API for:
- [X] [Amazon
Bedrock](https://docs.aws.amazon.com/bedrock/latest/userguide/inference-reasoning.html)
- [X]
[Anthropic](https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking)
- [X] [DeepSeek](https://api-docs.deepseek.com/guides/reasoning_model)
(via OpenAI module)
- [X] [Gemini](https://ai.google.dev/gemini-api/docs/thinking)
- [X] [Ollama](https://ollama.com/blog/thinking)

### API changes:
- `AiMessage`: added `thinking()` field to keep thinking/reasoning text
- `AiMessage`: added generic `attributes()` field to keep
thinking/reasoning signatures (for Bedrock, Anthropic, Gemini)
- `StreamingChatResponseHandler`: added
`onPartialThinking(PartialThinking)` callback
- `TokenStream`: added `onPartialThinking(Consumer<PartialThinking>)`
callback
- `BedrockChatModel` and `BedrockStreamingChatModel`: added
`returnThinking` and `sendThinking` parameters to the builder
- `AnthropicChatModel` and `AnthropicStreamingChatModel`: added
`returnThinking` and `sendThinking` parameters to the builder
- `GoogleAiGeminiChatModel` and `GoogleAiGeminiStreamingChatModel`:
added `returnThinking` and `sendThinking` parameters to the builder
- `OllamaChatModel` and `OllamaStreamingChatModel`: added `think` and
`returnThinking` parameters to the builder
- `OpenAiChatModel` and `OpenAiStreamingChatModel`: added
`returnThinking` parameter to the builder

## General checklist
- [X] There are no breaking changes
- [X] I have added unit and/or integration tests for my change
- [X] The tests cover both positive and negative cases
- [X] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [X] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
- [X] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)
- [X] I have added/updated [Spring Boot
starter(s)](https://github.com/langchain4j/langchain4j-spring):
https://github.com/langchain4j/langchain4j-spring/pull/133
2025-07-24 16:19:46 +02:00
Dmytro Liubarskyi d2c977b443 slight IT improvements 2025-07-09 11:32:16 +02:00
Dmytro Liubarskyi 39a504a83e Updated to the next development version 2025-06-18 19:36:52 +02:00