Skip to content

Refactor negative scenario tests. Add exception mapping to OpenAiOfficialChatModel #3310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

kpavlov
Copy link
Collaborator

@kpavlov kpavlov commented Jul 6, 2025

Issue

Reuse test logic by introducing abstract tests. Catch and fix missing exception mapping in OpenAiOfficialChatModel.

Change

  • Add error handling tests for AbstractChatModel and its implementations. Remove duplictie tests
  • Add test for streaming request in GoogleAiGeminiStreamingChatModelTest
  • Add OpenAiOfficialChatModelErrorsTest
  • Implemented missing OpenAiOfficialExceptionMapper to handle OpenAI API exceptions
  • Enhance HttpException constructors to carry underlying exception (cause)
  • Remove tests duplicating AbstractChatModelErrorsTest and it's children

General checklist

  • 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 and main modules, and they are all green

Checklist for adding new maven module

  • I have added my new module in the root pom.xml and langchain4j-bom/pom.xml

Checklist for adding new embedding store integration

  • 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

  • I have manually verified that the {NameOfIntegration}EmbeddingStore works correctly with the data persisted using the latest released version of LangChain4j

@kpavlov kpavlov added the Testing label Jul 6, 2025
@kpavlov kpavlov marked this pull request as ready for review July 6, 2025 10:14
@kpavlov kpavlov added the Gemini label Jul 6, 2025
@kpavlov kpavlov changed the title Refactor tests with ai-mock Refactor tests with ai-mock. Add exception mapping in OpenAiOfficialChatModel Jul 6, 2025
@kpavlov kpavlov added the bug Something isn't working label Jul 6, 2025
@kpavlov kpavlov changed the title Refactor tests with ai-mock. Add exception mapping in OpenAiOfficialChatModel Refactor negative tests with ai-mock. Add exception mapping in OpenAiOfficialChatModel Jul 6, 2025
@kpavlov kpavlov changed the title Refactor negative tests with ai-mock. Add exception mapping in OpenAiOfficialChatModel Refactor negative scenario tests with ai-mock. Add exception mapping in OpenAiOfficialChatModel Jul 6, 2025
@kpavlov kpavlov changed the title Refactor negative scenario tests with ai-mock. Add exception mapping in OpenAiOfficialChatModel Refactor negative scenario tests. Add exception mapping in OpenAiOfficialChatModel Jul 6, 2025
@kpavlov kpavlov changed the title Refactor negative scenario tests. Add exception mapping in OpenAiOfficialChatModel Refactor negative scenario tests. Add exception mapping to OpenAiOfficialChatModel Jul 6, 2025
LizeRaes and others added 7 commits July 6, 2025 22:01
…3313)

This PR contains the following updates:

| Package | Change | Age | Confidence |
|---|---|---|---|
|
[com.google.protobuf:protobuf-java-util](https://developers.google.com/protocol-buffers/)
([source](https://redirect.github.com/protocolbuffers/protobuf)) |
`3.25.5` -> `3.25.8` |
[![age](https://developer.mend.io/api/mc/badges/age/maven/com.google.protobuf:protobuf-java-util/3.25.8?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/maven/com.google.protobuf:protobuf-java-util/3.25.5/3.25.8?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>protocolbuffers/protobuf
(com.google.protobuf:protobuf-java-util)</summary>

###
[`v3.25.8`](https://redirect.github.com/protocolbuffers/protobuf/compare/v3.25.7...v3.25.8)

[Compare
Source](https://redirect.github.com/protocolbuffers/protobuf/compare/v3.25.7...v3.25.8)

###
[`v3.25.7`](https://redirect.github.com/protocolbuffers/protobuf/compare/v3.25.6...v3.25.7)

[Compare
Source](https://redirect.github.com/protocolbuffers/protobuf/compare/v3.25.6...v3.25.7)

###
[`v3.25.6`](https://redirect.github.com/protocolbuffers/protobuf/compare/v3.25.5...v3.25.6)

[Compare
Source](https://redirect.github.com/protocolbuffers/protobuf/compare/v3.25.5...v3.25.6)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/langchain4j/langchain4j).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNy4yIiwidXBkYXRlZEluVmVyIjoiNDEuMTcuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIl19-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
@kpavlov kpavlov force-pushed the tests-with-aimock branch from b5fdc1c to 0b9ac95 Compare July 6, 2025 19:01
Copy link
Member

@dliubarskyi dliubarskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kpavlov thanks a lot, good idea to abstract away error tests!

*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Execution(ExecutionMode.CONCURRENT)
public abstract class AbstractChatModelErrorsTest<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep the code written in Java (for all tests)

}

@Test
public fun should_handle_timeout() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original tests run for a few timeouts (1, 10, 100 ms) to make sure both connect and read timeouts are tested, could you please keep that logic?

@@ -38,6 +42,7 @@ abstract class OpenAiOfficialBaseChatModel {
protected TokenCountEstimator tokenCountEstimator;
protected List<ChatModelListener> listeners;
protected Set<Capability> supportedCapabilities;
protected int maxRetries = 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? maxRetries is set on the OpenAIClientAsync

public RuntimeException mapException(Throwable t) {
if (t instanceof com.openai.errors.OpenAIServiceException httpResponseException) {
final var statusCode = httpResponseException.statusCode();
return mapHttpStatusCode(new HttpException(statusCode, httpResponseException), statusCode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return mapHttpStatusCode(new HttpException(statusCode, httpResponseException), statusCode);
return mapHttpStatusCode(httpResponseException, statusCode);

why adding an extra layer of exceptions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once simplified, please remove new ctors in HttpException which are not required any more

final var statusCode = httpResponseException.statusCode();
return mapHttpStatusCode(new HttpException(statusCode, httpResponseException), statusCode);
} else if (t instanceof OpenAIIoException ioException) {
if (t.getCause() instanceof InterruptedIOException) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks strange, doesn't OpenAI SDK throw a proper timeout exception?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Gemini Testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants