Skip to content

Conversation

@ReneFabricius
Copy link

@ReneFabricius ReneFabricius commented Apr 24, 2025

Some ChatVertexAI responses contain empty usage_metadata in generation_info while message contains valid one. Breaking here does not allow for processing of the valid one and results in returning None.

Added processing of tokens details for ChatVertexAI as they have different name and different format than the currently processed details.
Keeping the details unprocessed leads to Pydantic validation error further down the line -> langfuse/langfuse#5468


Important

Improves usage_metadata parsing in ChatVertexAI responses by handling empty metadata and processing token details in langfuse/callback/langchain.py.

  • Behavior:
    • Modified _parse_usage in langfuse/callback/langchain.py to use .get('usage_metadata', None) for safer access.
    • Prevents early termination when usage_metadata is empty in ChatVertexAI responses.
    • Processes valid metadata from message field when generation_info metadata is empty.
  • Token Details:
    • Updated _parse_usage_model to handle prompt_tokens_details and candidates_tokens_details for ChatVertexAI.
    • Ensures correct parsing of token details to avoid Pydantic validation errors.

This description was created by Ellipsis for 10ceef6. You can customize this summary. It will automatically update as commits are pushed.


Greptile Summary

Disclaimer: Experimental PR review

Enhanced ChatVertexAI usage metadata parsing to handle empty metadata cases and properly process token details in different formats.

  • Modified _parse_usage in langfuse/callback/langchain.py to safely handle empty usage_metadata using .get() method
  • Added support for extracting valid metadata from message field when generation_info metadata is empty
  • Updated token details processing to handle ChatVertexAI-specific fields like prompt_tokens_details and candidates_tokens_details
  • Improved Pydantic validation compatibility by properly formatting token details before validation

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

@CLAassistant
Copy link

CLAassistant commented Apr 24, 2025

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

@ReneFabricius ReneFabricius marked this pull request as draft April 25, 2025 09:16
@ReneFabricius ReneFabricius marked this pull request as ready for review April 25, 2025 09:57
@ReneFabricius ReneFabricius changed the title Don't parse empty usage_metadata VertexAI usage metadata parsing Apr 25, 2025
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

1 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings | Greptile

@ReneFabricius
Copy link
Author

Hi @hassiebp , could you please look at this when you get a chance?

@hassiebp
Copy link
Contributor

hassiebp commented May 6, 2025

Thanks a lot for your contribution! Closing this in favor of #1181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants