Commit 3583b5c
Related to [KG-616](https://youtrack.jetbrains.com/issue/KG-616)
## Motivation and Context
This PR addresses critical issues encountered with SSE streaming for
both Google and OpenRouter clients, ensuring robust functionality and
better error handling.
1. **Google Client Fix:**
* **Problem:** The
[GoogleLLMClient](cci:2://file:///Users/ku76uh/Developer/jetbrains/fork/koog/prompt/prompt-executor/prompt-executor-clients/prompt-executor-google-client/src/commonMain/kotlin/ai/koog/prompt/executor/clients/google/GoogleLLMClient.kt:98:0-797:1)
was ignoring the `tools` parameter during streaming requests
([executeStreaming](cci:1://file:///Users/ku76uh/Developer/jetbrains/fork/koog/prompt/prompt-executor/prompt-executor-model/src/commonMain/kotlin/ai/koog/prompt/executor/model/PromptExecutor.kt:36:4-48:24)),
explicitly passing `emptyList()` instead. This broke tool usage for
streaming calls.
* **Solution:** We corrected this to specificially pass the `tools`
parameter to
[createGoogleRequest](cci:1://file:///Users/ku76uh/Developer/jetbrains/fork/koog/prompt/prompt-executor/prompt-executor-clients/prompt-executor-google-client/src/commonMain/kotlin/ai/koog/prompt/executor/clients/google/GoogleLLMClient.kt:277:4-446:5).
2. **OpenRouter Integration Test Fix:**
* **Problem:** Integration tests for OpenRouter streaming were failing
with `Socket is not connected` errors. This was traced back to the
default `Apache5` HTTP client engine used in tests, which has known
connection pooling issues with OpenRouter's non-standard Keep-Alive
comments (e.g., `: OPENROUTER PROCESSING`).
* **Solution:** We switched the integration test runtime dependency from
`ktor-client-apache5` to `ktor-client-cio`. The CIO (Coroutine-based
I/O) engine handles these SSE keep-alive comments correctly, resolving
the socket closure issues.
3. **Enhanced SSE Error Handling:**
* **Problem:** When a `SSEClientException` occurred (e.g., due to a 400
Bad Request or streaming interruption), the error handling logic in
[KtorKoogHttpClient](cci:2://file:///Users/ku76uh/Developer/jetbrains/fork/koog/http-client/http-client-ktor/src/commonMain/kotlin/ai/koog/http/client/ktor/KtorKoogHttpClient.kt:49:0-191:1)
would attempt to read `e.response?.bodyAsText()` directly. If the
response body was already consumed or unavailable (common in streaming
errors), this would throw a `DoubleReceiveException`, masking the
original error.
* **Solution:** We wrapped the `bodyAsText()` call in a `try-catch`
block. This allows us to successfully capture the error body for
handshake failures (like 400 Bad Request) while gracefully handling
cases where the body is unavailable (falling back to `null`), ensuring
the original `SSEClientException` is always propagated with maximum
available context.
## Breaking Changes
None. These are internal fixes and test configuration updates.
---
#### Type of the changes
- [ ] New feature (non-breaking change which adds functionality)
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [x] Tests improvement
- [ ] Refactoring
#### Checklist
- [x] The pull request has a description of the proposed change
- [x] I read the [Contributing
Guidelines](https://github.com/JetBrains/koog/blob/main/CONTRIBUTING.md)
before opening the pull request
- [x] The pull request uses **`develop`** as the base branch
- [x] Tests for the changes have been added
- [x] All new and existing tests passed
##### Additional steps for pull requests adding a new feature
- [x] An issue describing the proposed change exists
- [x] The pull request includes a link to the issue
- [ ] The change was discussed and approved in the issue
- [ ] Docs have been added / updated
---------
Co-authored-by: Malte Heuser <[email protected]>
1 parent 9072fea commit 3583b5c
File tree
6 files changed
+76
-4
lines changed- http-client/http-client-ktor/src/commonMain/kotlin/ai/koog/http/client/ktor
- integration-tests
- src/jvmTest/kotlin/ai/koog/integration/tests/executor
- prompt/prompt-executor/prompt-executor-clients/prompt-executor-google-client/src/commonMain/kotlin/ai/koog/prompt/executor/clients/google
6 files changed
+76
-4
lines changedLines changed: 8 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
164 | 170 | | |
165 | 171 | | |
166 | 172 | | |
167 | | - | |
| 173 | + | |
| 174 | + | |
168 | 175 | | |
169 | 176 | | |
170 | 177 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
Lines changed: 54 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
92 | | - | |
| 92 | + | |
93 | 93 | | |
94 | 94 | | |
95 | 95 | | |
| |||
1119 | 1119 | | |
1120 | 1120 | | |
1121 | 1121 | | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
1122 | 1175 | | |
1123 | 1176 | | |
1124 | 1177 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
124 | 130 | | |
125 | 131 | | |
126 | 132 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
118 | 124 | | |
119 | 125 | | |
120 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
175 | | - | |
| 175 | + | |
176 | 176 | | |
177 | 177 | | |
178 | 178 | | |
| |||
0 commit comments