Skip to content

Conversation

@t-miyak
Copy link

@t-miyak t-miyak commented Apr 24, 2025

Fixed to send usage of VertexAI correctly.

I encountered the following error while using VertexAI with LangChain.

10 validation errors for UpdateGenerationBody
usageDetails -> prompt_tokens_details
  value is not a valid integer (type=type_error.integer)
usageDetails -> candidates_tokens_details
  value is not a valid integer (type=type_error.integer)
usageDetails -> cache_tokens_details
  value is not a valid integer (type=type_error.integer)
usageDetails -> prompt_tokens
  field required (type=value_error.missing)
usageDetails -> completion_tokens
  field required (type=value_error.missing)
usageDetails -> total_tokens
  field required (type=value_error.missing)
usageDetails -> prompt_tokens_details -> modality
  value is not a valid integer (type=type_error.integer)
usageDetails -> input_tokens
  field required (type=value_error.missing)
usageDetails -> output_tokens
  field required (type=value_error.missing)
usageDetails -> total_tokens
  field required (type=value_error.missing)
Traceback (most recent call last):
  File "/app/.venv/lib/python3.13/site-packages/langfuse/client.py", line 2733, in update
    request = UpdateGenerationBody(**generation_body)
  File "/app/.venv/lib/python3.13/site-packages/pydantic/v1/main.py", line 347, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 10 validation errors for UpdateGenerationBody
usageDetails -> prompt_tokens_details
  value is not a valid integer (type=type_error.integer)
usageDetails -> candidates_tokens_details
  value is not a valid integer (type=type_error.integer)
usageDetails -> cache_tokens_details
  value is not a valid integer (type=type_error.integer)
usageDetails -> prompt_tokens
  field required (type=value_error.missing)
usageDetails -> completion_tokens
  field required (type=value_error.missing)
usageDetails -> total_tokens
  field required (type=value_error.missing)
usageDetails -> prompt_tokens_details -> modality
  value is not a valid integer (type=type_error.integer)
usageDetails -> input_tokens
  field required (type=value_error.missing)
usageDetails -> output_tokens
  field required (type=value_error.missing)
usageDetails -> total_tokens
  field required (type=value_error.missing)

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

langchain==0.3.24
langfuse==2.60.3
langchain-google-vertexai==2.0.21
google-cloud-aiplatform==1.90.0
google-cloud-core==2.4.3

Important

Fixes VertexAI response processing in LangChain integration by removing non-integer fields from usage data.

  • Behavior:
    • Fixes processing of VertexAI responses in langfuse/callback/langchain.py by removing non-integer fields from usage data.
    • Handles fields prompt_tokens_details, candidates_tokens_details, and cache_tokens_details.
  • Dependencies:
    • Removes langchain-google-vertexai dependency from pyproject.toml.

This description was created by Ellipsis 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.

  • Modified UpdateGenerationBody to properly validate Vertex AI's non-integer usage metadata fields
  • Updated langchain.py to filter out non-standard fields from Vertex AI responses before validation
  • Removed langchain-google-vertexai from dev dependencies to avoid version conflicts
  • Ensures proper handling of usage fields like prompt_tokens_details and candidates_tokens_details

The 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!

@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

2 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

@t-miyak
Copy link
Author

t-miyak commented Apr 24, 2025

Sorry, I'm not used to poerty and updated lock file, but now I restored it.

@t-miyak
Copy link
Author

t-miyak commented Apr 26, 2025

I've fixed a test for VertexAI and tested if it works.

@hassiebp
Copy link
Contributor

hassiebp commented May 6, 2025

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

@hassiebp hassiebp closed this May 6, 2025
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