response_format
value. class Step(BaseModel): explanation: str output: str class MathReasoning(BaseModel): steps: list[Step] final_answer: str
response_format=MathReasoning
message = [ {"role": "user", "content": "You are a helpful math tutor. Guide the user through the solution step by step."}, {"role": "user", "content": "how can I solve 8x + 7 = -23"} ]
from portkey_ai import Portkey from pydantic import BaseModel portkey = Portkey( api_key="api_key", virtual_key="key", ) class Step(BaseModel): explanation: str output: str class MathReasoning(BaseModel): steps: list[Step] final_answer: str completion = portkey.beta.chat.completions.parse( model="gemini-2.0-pro-exp-02-05", messages=[ { "role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step.", }, {"role": "user", "content": "how can I solve 8x + 7 = -23"}, ], response_format=MathReasoning, ) print(completion)
ParsedChatCompletion[MathReasoning](id='portkey-25129568-7600-4fc7-9d7d-4e5d20b4a707', choices=[ParsedChoice[MathReasoning](finish_reason='STOP', index=0, logprobs=None, message=ParsedChatCompletionMessage[MathReasoning](content='{\n "final_answer": "-3.75",\n "steps": [\n {\n "explanation": "The goal is to isolate x on one side of the equation. First, subtract 7 from both sides.",\n "output": "8x + 7 - 7 = -23 - 7"\n },\n {\n "explanation": "Simplify both sides of the equation.",\n "output": "8x = -30"\n },\n {\n "explanation": "Divide both sides by 8 to isolate x.",\n "output": "8x / 8 = -30 / 8"\n },\n {\n "explanation": "Simplify to get the value of x.",\n "output": "x = -3.75"\n }\n ]\n}', refusal=None, role='assistant', function_call=None, tool_calls=[], parsed=MathReasoning(steps=[Step(explanation='The goal is to isolate x on one side of the equation. First, subtract 7 from both sides.', output='8x + 7 - 7 = -23 - 7'), Step(explanation='Simplify both sides of the equation.', output='8x = -30'), Step(explanation='Divide both sides by 8 to isolate x.', output='8x / 8 = -30 / 8'), Step(explanation='Simplify to get the value of x.', output='x = -3.75')], final_answer='-3.75')))], created=1739961346, model='Unknown', object='chat_completion', service_tier=None, system_fingerprint=None, usage=CompletionUsage(completion_tokens=188, prompt_tokens=52, total_tokens=240), provider='vertex-ai')
from portkey_ai import createHeaders from openai import OpenAI from pydantic import BaseModel client = OpenAI( api_key="api-key", #skip the error. base_url="gateway_url", default_headers=createHeaders( api_key="key", virtual_key="key", ), ) class Step(BaseModel): explanation: str output: str class MathReasoning(BaseModel): steps: list[Step] final_answer: str completion = client.beta.chat.completions.parse( model="gemini-2.0-pro-exp-02-05", messages=[ { "role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step.", }, {"role": "user", "content": "how can I solve 8x + 7 = -23"}, ], response_format=MathReasoning, ) print(completion)
{ "strategy" : { "mode" : "loadbalance" }, "request_timeout" : 60000, "targets" : [ { "provider" : "vertex-ai", "vertex_region" : "us-central1", "override_params" : { "model" : "gemini-2.0-pro-exp-02-05", "safety_settings" : [ { "category" : "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold" : "BLOCK_NONE" }, { "category" : "HARM_CATEGORY_HARASSMENT", "threshold" : "BLOCK_NONE" }, { "category" : "HARM_CATEGORY_HATE_SPEECH", "threshold" : "BLOCK_NONE" }, { "category" : "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold" : "BLOCK_NONE" } ] }, "api_key" : api_key, "vertex_project_id" : vertex_project_id, "weight" : 1 } ] }
request_timeout
would be x-portkey-request-timeout
and similarly for others in kebab-case