-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Fix/litellm finish reason 3109 #3319
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
Closed
Closed
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
cb92b70
fix: propagate finish_reason from LiteLLM responses
aperepel 5c43ac2
refactor: address code review feedback
aperepel ac27a3a
Update src/google/adk/models/lite_llm.py
aperepel 3924bcb
Update src/google/adk/telemetry/tracing.py
aperepel ab6f577
fix: update finish_reason type hint to support both enum and string
aperepel 7996112
feat: map LiteLLM finish_reason strings to FinishReason enum
aperepel a1c0938
feat: map LiteLLM finish_reason strings to FinishReason enum
aperepel 692af95
refactor: address bot review suggestions
aperepel 538a5b0
refactor: use _FINISH_REASON_MAPPING directly in tests
aperepel cb44fb4
refactor: remove unused Union import from llm_response.py
aperepel 0becccf
Apply suggestion from @gemini-code-assist[bot]
aperepel 63d8b71
fix: apply review suggestions for litellm finish_reason
lizzij 76a8f24
Add e2e test for litellm finish reason
lizzij c736928
fix python formatting
lizzij ce025f4
Merge branch 'main' into fix/litellm-finish-reason-3109
lizzij 8e6da69
Merge branch 'main' into fix/litellm-finish-reason-3109
xuanyang15 9622dee
add license
lizzij b8b49fb
fix sample reasoning agent
lizzij bb12952
fix python format
lizzij 7171a09
remove agent
lizzij 90466f6
Merge branch 'main' into fix/litellm-finish-reason-3109
lizzij fe9dbcc
Merge branch 'main' into fix/litellm-finish-reason-3109
lizzij da6ed0a
Merge branch 'main' into fix/litellm-finish-reason-3109
lizzij 840b126
Merge branch 'main' into fix/litellm-finish-reason-3109
lizzij 5dbc5a7
Merge branch 'main' into fix/litellm-finish-reason-3109
lizzij b159d59
Merge branch 'main' into fix/litellm-finish-reason-3109
lizzij 02adaa2
Merge branch 'main' into fix/litellm-finish-reason-3109
lizzij File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Finish Reason Test Agent | ||
|
|
||
| This sample contains a script to verify that the `finish_reason` from a LiteLLM model is correctly propagated to the `LlmResponse` object. | ||
|
|
||
| The script is configured to use the `openai/gpt-3.5-turbo` model through LiteLLM. It sets `max_tokens=50` to force the model to stop execution due to length constraints. An `after_model_callback` is used to inspect the `response.finish_reason` and verify that it is `length`. | ||
|
|
||
| ## Running the test | ||
|
|
||
| To run this sample, you will need to have an OpenAI API key set as an environment variable. Then, run the `agent.py` script directly. | ||
|
|
||
| ```bash | ||
| export OPENAI_API_KEY="your-api-key-here" | ||
| python agent.py | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import asyncio | ||
| import os | ||
|
|
||
| from google.adk.agents import Agent | ||
| from google.adk.agents.callback_context import CallbackContext | ||
| from google.adk.models.lite_llm import LiteLlm | ||
| from google.adk.models.llm_response import LlmResponse | ||
| from google.adk.runners import Runner | ||
| from google.adk.sessions import InMemorySessionService | ||
| from google.genai import types | ||
|
|
||
|
|
||
| def create_inspector(): | ||
| """Callback to capture finish_reason.""" | ||
| captured = {"finish_reason": None} | ||
|
|
||
| def inspector( | ||
| callback_context: CallbackContext, llm_response: LlmResponse | ||
| ) -> LlmResponse: | ||
| captured["finish_reason"] = llm_response.finish_reason | ||
| return llm_response | ||
|
|
||
| inspector.captured = captured | ||
| return inspector | ||
|
|
||
|
|
||
| async def test(): | ||
| # Create model with low max_tokens to trigger truncation | ||
| model = LiteLlm( | ||
| model="gpt-3.5-turbo", | ||
| api_key=os.environ.get("OPENAI_API_KEY"), | ||
| max_tokens=50, # Intentionally low | ||
| ) | ||
|
|
||
| inspector = create_inspector() | ||
|
|
||
| agent = Agent( | ||
| model=model, | ||
| name="test", | ||
| instruction="Provide detailed explanations.", | ||
| after_model_callback=inspector, | ||
| ) | ||
|
|
||
| session_service = InMemorySessionService() | ||
| runner = Runner(app_name="test", agent=agent, session_service=session_service) | ||
|
|
||
| await session_service.create_session( | ||
| app_name="test", | ||
| user_id="user", | ||
| session_id="session", | ||
| state={}, | ||
| ) | ||
|
|
||
| message = types.Content( | ||
| role="user", | ||
| parts=[types.Part(text="Explain quantum computing in detail.")], | ||
| ) | ||
|
|
||
| async for _ in runner.run_async( | ||
| user_id="user", session_id="session", new_message=message | ||
| ): | ||
| pass | ||
|
|
||
| print(f"finish_reason: {inspector.captured['finish_reason']}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(test()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.