How to get OpenAI with_structured_output token usage with Pydantic Schema #28704
Replies: 3 comments 1 reply
-
Hi, i am also interested by knowing how to get get token usage with structured outputs using json schema. i can't find a clean way to do it neither |
Beta Was this translation helpful? Give feedback.
-
In your example, |
Beta Was this translation helpful? Give feedback.
-
I am using ChatBedrock with Claude Sonnet 3.5. The structured_llm = llm.with_structured_output(CustomPromptResponse, include_raw=True)
response = structured_llm.invoke(user_prompt)
msg: AIMessage = response["raw"]
response: CustomPromptResponse = response["parsed"]
token_usage = msg.usage_metadata
log.debug(f"token usage:", extra=token_usage)
recommendation = response.feedback + "\n" + response.recommendation And I hope this helps. |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
Outputs:
Here are the outputs I received:
System Info
Problem:
I want to achieve the output in the second format (structured output matching the user input), but when I use the second method, I am unable to access
input_tokens
or token usage details.Request:
Can you provide guidance or an example that demonstrates how to:
input_tokens
at the same time?Thank you!
Beta Was this translation helpful? Give feedback.
All reactions