Fix Langchain ChatVertexAI #1175
Closed
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.
After getting a massive headache while working on our lovely application, I have decided to finally fix this problem in the Langfuse Python SDK.
Basically using ChatVertexAI together with Langfuse will end up providing you with humongous amounts of pain in form of spamming your terminal logs with pydantic error messages.
On top the token usage informations won't be displayed in Langfuse - Hooray.
Here a minimal example to reproduce the issue with the latest
langfusepypi package (version 2.60.3):The terminal output:
The fix has been tested with both the above GoogleVertexAI config and also an AzureChatOpenAI config. Please retest.
Yall need to use some more Google models! (that's the secret sauce :D)
Best regards,
Pao
Important
Fixes pydantic validation errors and improves model name extraction for ChatVertexAI in Langfuse by updating
_parse_usage()and_parse_model()functions.langfuse/callback/langchain.pyby ensuring token usage details are integers in_parse_usage()._parse_usage()to handle Vertex AI'susage_metadataand log warnings if conversion to integer fails._parse_model()to extract model names fromresponse_metadatafor Vertex AI._parse_usage()to extract and convert token counts fromusage_metadata._parse_model()to checkresponse_metadatafor model names._parse_usage().This description was created by
for 3e3460b. You can customize this summary. It will automatically update as commits are pushed.
Greptile Summary
Disclaimer: Experimental PR review
Added support for Google's Vertex AI (Gemini) model in the Langfuse Python SDK, fixing token usage parsing and error handling issues when using ChatVertexAI with Langfuse callbacks.
langfuse/callback/langchain.pyto properly parse Vertex AI's unique response structure for token usage metrics💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!