Skip to content

Commit 872aea7

Browse files
committed
tweak structured output example
1 parent 3ebca20 commit 872aea7

File tree

2 files changed

+108
-34
lines changed

2 files changed

+108
-34
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
-- Example: Structured output with Gemini
2+
-- Requires GEMINI_API_KEY environment variable
3+
4+
local Gemini = require("openai.compat.gemini")
5+
local cjson = require("cjson")
6+
7+
local client = Gemini.new(os.getenv("GEMINI_API_KEY"))
8+
9+
-- Define a JSON schema for extracting information about a person
10+
local person_schema = {
11+
type = "json_schema",
12+
json_schema = {
13+
name = "person_info",
14+
strict = true,
15+
schema = {
16+
type = "object",
17+
properties = {
18+
name = {
19+
type = "string",
20+
description = "The person's full name"
21+
},
22+
age = {
23+
type = "number",
24+
description = "The person's age in years"
25+
},
26+
occupation = {
27+
type = "string",
28+
description = "The person's job or profession"
29+
},
30+
skills = {
31+
type = "array",
32+
items = { type = "string" },
33+
description = "List of skills the person has"
34+
}
35+
},
36+
required = {"name", "age", "occupation", "skills"},
37+
additionalProperties = false
38+
}
39+
}
40+
}
41+
42+
-- Request structured output
43+
local status, response = client:chat({
44+
{
45+
role = "user",
46+
content = "Extract information about this person: John Smith is a 35-year-old software engineer. He knows Python, JavaScript, and Go."
47+
}
48+
}, {
49+
response_format = person_schema
50+
})
51+
52+
if status ~= 200 then
53+
io.stderr:write("Error: " .. status .. "\n")
54+
io.stderr:write(cjson.encode(response) .. "\n")
55+
os.exit(1)
56+
end
57+
58+
local content = response.choices[1].message.content
59+
print(content)
60+
-- Verify it parses as valid JSON
61+
assert(cjson.decode(content), "Failed to parse JSON")
62+
63+
io.stderr:write("\n--- Using ChatSession with structured output ---\n\n")
64+
65+
-- You can also use structured output with a chat session
66+
local recipe_schema = {
67+
type = "json_schema",
68+
json_schema = {
69+
name = "recipe",
70+
strict = true,
71+
schema = {
72+
type = "object",
73+
properties = {
74+
dish_name = { type = "string" },
75+
prep_time_minutes = { type = "number" },
76+
ingredients = {
77+
type = "array",
78+
items = { type = "string" }
79+
},
80+
steps = {
81+
type = "array",
82+
items = { type = "string" }
83+
}
84+
},
85+
required = {"dish_name", "prep_time_minutes", "ingredients", "steps"},
86+
additionalProperties = false
87+
}
88+
}
89+
}
90+
91+
local chat = client:new_chat_session({
92+
response_format = recipe_schema
93+
})
94+
95+
local recipe_json = chat:send("Give me a simple recipe for scrambled eggs")
96+
print(recipe_json)
97+
-- Verify it parses as valid JSON
98+
assert(cjson.decode(recipe_json), "Failed to parse JSON")

examples/responses/structured_output.lua

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,37 +52,27 @@ local status, response = client:create_response(
5252
)
5353

5454
if status ~= 200 then
55-
print("Error:", status)
56-
print(cjson.encode(response))
55+
io.stderr:write("Error: " .. status .. "\n")
56+
io.stderr:write(cjson.encode(response) .. "\n")
5757
os.exit(1)
5858
end
5959

60-
print("Response ID:", response.id)
61-
print("Raw output:")
62-
6360
-- Extract the text content from the response
6461
for _, output_item in ipairs(response.output or {}) do
6562
if output_item.content then
6663
for _, content_item in ipairs(output_item.content) do
6764
if content_item.type == "output_text" and content_item.text then
6865
print(content_item.text)
69-
70-
-- Parse and display the structured data
71-
local parsed = cjson.decode(content_item.text)
72-
print("\nParsed structured data:")
73-
print(" Name:", parsed.name)
74-
print(" Age:", parsed.age)
75-
print(" Occupation:", parsed.occupation)
76-
print(" Skills:", table.concat(parsed.skills, ", "))
77-
print(" Employed:", parsed.is_employed and "Yes" or "No")
66+
-- Verify it parses as valid JSON
67+
assert(cjson.decode(content_item.text), "Failed to parse JSON")
7868
end
7969
end
8070
end
8171
end
8272

83-
print("\n--- Using ResponsesChatSession with structured output ---\n")
73+
io.stderr:write("\n--- Using ResponsesChatSession with structured output ---\n\n")
8474

85-
-- You can also use structured outputs with the chat session by passing options to create_response
75+
-- You can also use structured outputs with the chat session
8676
local session = client:new_response_chat_session()
8777

8878
local recipe_schema = {
@@ -118,29 +108,15 @@ local recipe_schema = {
118108
}
119109
}
120110

121-
-- Use create_response directly to pass the text format option
122111
local recipe_response, err = session:create_response("Give me a recipe for chocolate chip cookies", {
123112
text = { format = recipe_schema }
124113
})
125114

126115
if recipe_response then
127-
print("Recipe response:")
128116
print(recipe_response.output_text)
129-
130-
local recipe = cjson.decode(recipe_response.output_text)
131-
print("\nFormatted recipe:")
132-
print("Dish:", recipe.dish_name)
133-
print("Prep time:", recipe.prep_time_minutes, "minutes")
134-
print("Cook time:", recipe.cook_time_minutes, "minutes")
135-
print("Servings:", recipe.servings)
136-
print("\nIngredients:")
137-
for _, ing in ipairs(recipe.ingredients) do
138-
print(" -", ing.amount, ing.item)
139-
end
140-
print("\nSteps:")
141-
for i, step in ipairs(recipe.steps) do
142-
print(string.format(" %d. %s", i, step))
143-
end
117+
-- Verify it parses as valid JSON
118+
assert(cjson.decode(recipe_response.output_text), "Failed to parse JSON")
144119
else
145-
print("Error:", err)
120+
io.stderr:write("Error: " .. tostring(err) .. "\n")
121+
os.exit(1)
146122
end

0 commit comments

Comments
 (0)