run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
36.09k stars 5.14k forks source link

[Bug]: AttributeError: 'GenerateContentResponse' object has no attribute 'usage_metadata' #16555

Open saleemdjima opened 1 hour ago

saleemdjima commented 1 hour ago

Bug Description

I'm having an AttributeError when i try to use JSONQueryEngine with Gemini Api. I'm using google-generativeai version 0.5.2. The latest version is having issue with llamaindex

Version

0.11.17

Steps to Reproduce

llm=Gemini()

raw_query_engine = JSONQueryEngine( json_value=json_value, json_schema=json_schema, llm=llm, synthesize_response=False )

raw_response = raw_query_engine.query( "What comments has Jerry been writing?", )

print(raw_response)

Relevant Logs/Tracbacks

No response

dosubot[bot] commented 1 hour ago

Hey @saleemdjima! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this together!

To resolve the AttributeError: 'GenerateContentResponse' object has no attribute 'usage_metadata', you need to check if the usage_metadata attribute exists before accessing it. You can modify your code as follows:

In the functions where you handle the GenerateContentResponse, add a check for the usage_metadata attribute:

def completion_from_gemini_response(response):
    top_candidate = response.candidates[0]
    _error_if_finished_early(top_candidate)

    raw = {
        **(type(top_candidate).to_dict(top_candidate)),
        **(type(response.prompt_feedback).to_dict(response.prompt_feedback)),
    }
    # Check if 'usage_metadata' exists before accessing it
    if hasattr(response, 'usage_metadata') and response.usage_metadata:
        raw["usage_metadata"] = type(response.usage_metadata).to_dict(response.usage_metadata)
    return CompletionResponse(text=response.text, raw=raw)

Similarly, apply the same check in the chat_from_gemini_response function:

def chat_from_gemini_response(response):
    top_candidate = response.candidates[0]
    _error_if_finished_early(top_candidate)

    raw = {
        **(type(top_candidate).to_dict(top_candidate)),
        **(type(response.prompt_feedback).to_dict(response.prompt_feedback)),
    }
    # Check if 'usage_metadata' exists before accessing it
    if hasattr(response, 'usage_metadata') and response.usage_metadata:
        raw["usage_metadata"] = type(response.usage_metadata).to_dict(response.usage_metadata)
    role = ROLES_FROM_GEMINI[top_candidate.content.role]
    return ChatResponse(message=ChatMessage(role=role, content=response.text), raw=raw)

This modification ensures that the code only attempts to access usage_metadata if it exists, preventing the AttributeError [1].

To continue talking to Dosu, mention @dosu.