Welcome to Portkey Forum

Updated 2 days ago

Vertex structured outputs

Hi everyone!
I'm working with the openai client + portkey and I can't send structured_output type queries to vertex ai gemini models.
However sending the same queries to Azure Openai hosted model o1 works fine.
I'm attaching the request I send and the error I receive.
I'm using the openai==1.61.1 package version & 1.9.5 gateway proxy.
Will be glad for your assistance πŸ™‚
1
V
a
b
25 comments
Hi @artyabra, checking this!
Hey, did you guys had a chance to have a look?
@Vrushank | Portkey ?
Hi @artyabra so sorry for the delay here - let us get back to you with the docs on this shortly. @sega
Hi, @artyabra. Is it possible for you to share the Python code you're using. Or atleast share the pydantic model you're using for the response_format value.

I'll also try taking a look at from my side as well maybe a share a working example as well.
Hi, there's no problem in sharing, I will send you first the response_format, since it's easier:

Plain Text
class Step(BaseModel):
    explanation: str
    output: str

class MathReasoning(BaseModel):
    steps: list[Step]
    final_answer: str


I'm sending response_format=MathReasoning
It's an example from openai docs
Sure, thanks for sharing the info. Will revert back with the solution soon.
ah and the message if you want is also like in the openai docs:

Plain Text
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"}
]
Just tested the code, it's working fine for me.
Plain Text
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)
This is the code I have tried,
Plain Text
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')

this is the response I've got
portkey-ai==1.9.1 SDK Version.
Im not using the portkey client, Im using the openai client
got it, let me check that too.
yea, I got response with openai as well.
openai==1.63.2
can you send me the request it's generating for you and being sent to gateway proxy?
Plain Text
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)

This the code block I have used.
let me know if that did worked or not, can also get on a call to quickly understand more.
Im not familiar with create_headers method.
Can you try with these default_headers?

Plain Text
{
  "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
  } ]
}
I also dont have a virtual key
I sent the x-portkey-config key in default_headers
if you're not usng the createHeaders method, you have to format you header keys manually
request_timeout would be x-portkey-request-timeout and similarly for others in kebab-case
The root cause is because we're using pydantic v1 package version.
When installing pydantic v2 it works.
However it's hard for us to migrate pydantic to v2.
Thank you @b4s36t4 for identifying the root cause!
Add a reply
Sign up and join the conversation on Discord