fix: process Vertex AI response properly for usage#1173
Closed
t-miyak wants to merge 7 commits intolangfuse:mainfrom
Closed
fix: process Vertex AI response properly for usage#1173t-miyak wants to merge 7 commits intolangfuse:mainfrom
t-miyak wants to merge 7 commits intolangfuse:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
LGTM
2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile
Contributor
There was a problem hiding this comment.
LGTM
1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile
Author
|
Sorry, I'm not used to poerty and updated lock file, but now I restored it. |
Author
|
I've fixed a test for VertexAI and tested if it works. |
Contributor
|
Thanks a lot for your contribution! Closing this in favor of #1181 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixed to send usage of VertexAI correctly.
I encountered the following error while using VertexAI with LangChain.
This seems to occur when using the newer version of langchain-google-vertexai, and was caused by the UsageMetadata in VertexAI API responses containing fields that are not integers.
Example: json of response (`langchain_core.outputs.LLMResult`)
{ "generations": [ [ { "text": "", "generation_info": { "is_blocked": false, "safety_ratings": [], "usage_metadata": { "prompt_token_count": 4464, "candidates_token_count": 18, "total_token_count": 4482, "prompt_tokens_details": [ { "modality": 1, "token_count": 4464 } ], "candidates_tokens_details": [ { "modality": 1, "token_count": 18 } ], "cached_content_token_count": 0, "cache_tokens_details": [] }, "finish_reason": "STOP", "avg_logprobs": -0.000507740666055017 }, "type": "ChatGeneration", "message": { "content": "", "additional_kwargs": { "function_call": { "name": "xxxxx", "arguments": "{\"aaaaa\": \"bbbbb\"}" } }, "response_metadata": { "is_blocked": false, "safety_ratings": [], "usage_metadata": { "prompt_token_count": 4464, "candidates_token_count": 18, "total_token_count": 4482, "prompt_tokens_details": [ { "modality": 1, "token_count": 4464 } ], "candidates_tokens_details": [ { "modality": 1, "token_count": 18 } ], "cached_content_token_count": 0, "cache_tokens_details": [] }, "finish_reason": "STOP", "avg_logprobs": -0.000507740666055017, "model_name": "gemini-2.0-flash-001" }, "type": "ai", "name": null, "id": "run-3a8c2d0d-9f67-4e04-a58e-6dafeaeb9202-0" } } ] ], "llm_output": null, "run": null, "type": "LLMResult" }Since these fields are not necessary for Usage, I added processing to remove them.
my dependencies
Important
Fixes VertexAI response processing in LangChain integration by removing non-integer fields from usage data.
langfuse/callback/langchain.pyby removing non-integer fields from usage data.prompt_tokens_details,candidates_tokens_details, andcache_tokens_details.langchain-google-vertexaidependency frompyproject.toml.This description was created by
for 7270a42. You can customize this summary. It will automatically update as commits are pushed.
Greptile Summary
Disclaimer: Experimental PR review
This PR addresses validation errors when processing Vertex AI responses in the Langfuse Python SDK by modifying how usage metadata is handled.
UpdateGenerationBodyto properly validate Vertex AI's non-integer usage metadata fieldslangchain.pyto filter out non-standard fields from Vertex AI responses before validationlangchain-google-vertexaifrom dev dependencies to avoid version conflictsprompt_tokens_detailsandcandidates_tokens_detailsThe changes focus on maintaining compatibility with newer versions of langchain-google-vertexai while ensuring proper validation of usage data structures.
💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!