Give a model functions to call, ask for JSON that follows your schema, send it images, or read how it reasoned, with the same OpenAI client you use for plain chat. Each of these works on a model that shows the matching tag on its page: Tools, JSON, Vision, or Reasoning. Check the tags before you choose a model; see Choose a model.
The examples use Python and a client set up as in Call a model with the OpenAI
SDK. They call Gemma 4 31B, model ID
google/gemma-4-31b-it, which shows all four tags.
Let the model call your functions
Section titled “Let the model call your functions”Describe each function in tools, with a name, a description, and its parameters as a JSON schema.
tool_choice decides whether the model calls one. With auto, the model decides for itself, and
with none, it calls no function. required makes it call at least one, and naming a function
makes it call that one.
import json
tools = [ { "type": "function", "function": { "name": "get_weather", "description": "Get the current weather in a city.", "parameters": { "type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"], }, }, }]messages = [{"role": "user", "content": "What is the weather in Jaipur?"}]
response = client.chat.completions.create( model="google/gemma-4-31b-it", messages=messages, tools=tools, tool_choice="auto",)message = response.choices[0].messageTo make the model call one function in particular, name it in tool_choice:
tool_choice={"type": "function", "function": {"name": "get_weather"}}To find out whether the model called a function, check the message’s tool calls, not its finish
reason: the finish reason can be stop even when the model called a function. A reply can ask for
several functions at once. Run each one, and send each result back as its own message, with the role
tool and the ID of the call it answers. Then ask the model again for its answer:
if message.tool_calls: messages.append(message) for call in message.tool_calls: args = json.loads(call.function.arguments) result = {"city": args["city"], "temperature_c": 31} # your own function's result messages.append( {"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)} ) response = client.chat.completions.create( model="google/gemma-4-31b-it", messages=messages, tools=tools )
print(response.choices[0].message.content)Get JSON that follows your schema
Section titled “Get JSON that follows your schema”Set response_format to a JSON schema, and the reply’s content is JSON that follows it. Give every
array in the schema a maximum number of items, and set max_tokens high enough to leave room for
the model’s reasoning as well as the JSON:
import json
response = client.chat.completions.create( model="google/gemma-4-31b-it", messages=[{"role": "user", "content": "Name three cities in India and their states."}], response_format={ "type": "json_schema", "json_schema": { "name": "cities", "schema": { "type": "object", "properties": { "cities": { "type": "array", "maxItems": 3, "items": { "type": "object", "properties": { "city": {"type": "string"}, "state": {"type": "string"}, }, "required": ["city", "state"], "additionalProperties": False, }, } }, "required": ["cities"], "additionalProperties": False, }, }, }, max_tokens=2048,)data = json.loads(response.choices[0].message.content)When any valid JSON will do and you have no schema, set the format to a JSON object instead, and ask for JSON in your prompt:
response_format={"type": "json_object"}Send an image
Section titled “Send an image”Put the image in a user message as an image part, next to your text. The image can be a public URL or a data URI that carries the image itself:
import base64
with open("photo.png", "rb") as f: image_url = "data:image/png;base64," + base64.b64encode(f.read()).decode()
response = client.chat.completions.create( model="google/gemma-4-31b-it", messages=[ { "role": "user", "content": [ {"type": "text", "text": "What is in this picture?"}, {"type": "image_url", "image_url": {"url": image_url}}, ], } ],)print(response.choices[0].message.content)For an image on the web, pass its address, such as https://example.com/photo.png, as the URL
instead. An image sent to a model without Vision is refused with a 400 error.
Read a model’s reasoning
Section titled “Read a model’s reasoning”A model with Reasoning works through the problem before it answers, and returns that reasoning
in a field of its own beside the answer. The field is called reasoning on some models and
reasoning_content on others, so read whichever is present:
response = client.chat.completions.create( model="google/gemma-4-31b-it", messages=[{"role": "user", "content": "Is 391 a prime number?"}], max_tokens=4096,)message = response.choices[0].messagereasoning = getattr(message, "reasoning_content", None) or getattr(message, "reasoning", None)print(reasoning)print(message.content)Reasoning tokens count toward max_tokens. If the reasoning uses them all, the answer comes back
empty and the finish reason is length; send the request again with a higher max_tokens.
If a request that uses any of these features is refused, Troubleshooting TIR Token Factory explains each error.
Was this page helpful?