Commit b9625bf
fix: Litellm preserve streamed reasoning deltas in LiteLLM adapter
Merge #4952
**Please ensure you have read the [contribution guide](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) before creating a pull request.**
### Link to Issue or Description of Change
**1. Link to an existing issue (if applicable):**
**2. Or, if no issue exists, describe the change:**
Fixes: #5645
**Problem:**
In `LiteLlm` message conversion, reasoning parts were combined with newline
injection:
`reasoning_content = _NEW_LINE.join(text for text in reasoning_texts if text)`
For providers that stream reasoning in delta fragments (for example, vLLM-style
reasoning chunks), this mutates the original stream by inserting extra
separators. The reconstructed reasoning can differ compared to provider
output.
**Solution:**
Preserve reasoning text exactly as streamed by concatenating fragments without
adding separators:
`reasoning_content = "".join(text for text in reasoning_texts if text)`
This avoids corruption of chunked reasoning while still preserving explicit
newlines already present in fragments.
Also added targeted regression tests to lock behavior:
- `test_content_to_message_param_preserves_chunked_reasoning_deltas`
- `test_content_to_message_param_preserves_reasoning_newlines`
### Testing Plan
**Unit Tests:**
- [x] I have added or updated unit tests for my change.
- [x] All unit tests pass locally.
Summary of local `pytest` runs:
1. `python -m pytest tests/unittests/models/test_litellm.py -k "content_to_message_param_assistant_thought_and_content_message or preserves_chunked_reasoning_deltas or preserves_reasoning_newlines"`
- Result: `3 passed, 244 deselected`
2. `python -m pytest tests/unittests/models/test_litellm.py -k "preserves_chunked_reasoning_deltas or preserves_reasoning_newlines"`
- Result: `2 passed, 245 deselected`
**Manual End-to-End (E2E) Tests:**
### Checklist
- [x] I have read the [CONTRIBUTING.md](https://github.com/google/adk-python/blob/main/CONTRIBUTING.md) document.
- [x] I have performed a self-review of my own code.
- [x] I have commented my code, particularly in hard-to-understand areas.
- [x] I have added tests that prove my fix is effective or that my feature works.
- [x] New and existing unit tests pass locally with my changes.
- [ ] I have manually tested my changes end-to-end.
- [ ] Any dependent changes have been merged and published in downstream modules.
### Additional context
Scope is intentionally minimal and low risk:
- 1-line behavior change in reasoning-content reconstruction.
- 2 regression tests added.
- Anthropic `thinking_blocks` path is unchanged.
Co-authored-by: George Weale <gweale@google.com>
COPYBARA_INTEGRATE_REVIEW=#4952 from lorenzbaraldi:fix/reasoning-accumulation 5a09d55
PiperOrigin-RevId: 9382608361 parent 0a9ce0f commit b9625bf
2 files changed
Lines changed: 35 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1030 | 1030 | | |
1031 | 1031 | | |
1032 | 1032 | | |
1033 | | - | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
1034 | 1036 | | |
1035 | 1037 | | |
1036 | 1038 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2217 | 2217 | | |
2218 | 2218 | | |
2219 | 2219 | | |
| 2220 | + | |
| 2221 | + | |
| 2222 | + | |
| 2223 | + | |
| 2224 | + | |
| 2225 | + | |
| 2226 | + | |
| 2227 | + | |
| 2228 | + | |
| 2229 | + | |
| 2230 | + | |
| 2231 | + | |
| 2232 | + | |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
| 2236 | + | |
| 2237 | + | |
| 2238 | + | |
| 2239 | + | |
| 2240 | + | |
| 2241 | + | |
| 2242 | + | |
| 2243 | + | |
| 2244 | + | |
| 2245 | + | |
| 2246 | + | |
| 2247 | + | |
| 2248 | + | |
| 2249 | + | |
| 2250 | + | |
| 2251 | + | |
2220 | 2252 | | |
2221 | 2253 | | |
2222 | 2254 | | |
| |||
0 commit comments