As of mid-day May 2, the endpoint https://generativelanguage.googleapis.com/v1beta/ appends this object to server responses after the “candidates” array:
"usageMetadata": {
"candidatesTokenCount": 27
}
This post has the bug tag because that means the messages returned from the endpoint no longer conform to the specification for GenerateContentResponse
This change broke application code, for example the go library started showing
proto: (line 34:3): unknown field "usageMetadata"
(This is reported in proto: (line 34:3): unknown field "usageMetadata" · Issue #97 · google/generative-ai-go · GitHub )
Two questions:
- Is the new observed behavior a permanent change, and the documentation for
GenerateContentResponse
will be updated soon? - The specification for
Candidate
(Candidate | Google AI for Developers | Google for Developers) includes the member “tokenCount”: integer, which has been missing in the responses from the v1beta endpoint (this has been previously reported and is a known bug). Based on its placement, we are currently assuming thatcandidatesTokenCount
(inusageMetadata
) will contain the sum of thetokenCount
over the array ofCandidate
. Since the candidate count is currently fixed at 1, thecandidatesTokenCount
may be a suitable substitute for the missingtokenCount
field. Is that the intention?
Receiving a response from an authoritative source is valuable for developers using the API.