@@ -123,6 +123,7 @@ Template variables make it easy to reuse prompts with different values. This is
123
123
from google import genai
124
124
import instructor
125
125
from pydantic import BaseModel
126
+ from google.genai import types
126
127
127
128
128
129
# Define your Pydantic model
@@ -138,37 +139,49 @@ client = instructor.from_genai(client, mode=instructor.Mode.GENAI_TOOLS)
138
139
# Single string (converted to user message)
139
140
response = client.chat.completions.create(
140
141
model = " gemini-2.0-flash-001" ,
141
- messages = [" Jason is 25 years old" ],
142
+ messages = [" {{ name }} is {{ age }} years old" ],
142
143
response_model = User,
144
+ context = {
145
+ " name" : " Jason" ,
146
+ " age" : 25 ,
147
+ },
143
148
)
144
149
145
150
print (response)
146
- # > name='Jason' age=25
151
+ # > name='Jason' age=25
147
152
148
153
# Standard format
149
154
response = client.chat.completions.create(
150
155
model = " gemini-2.0-flash-001" ,
151
- messages = [{" role" : " user" , " content" : " Jason is 25 years old" }],
156
+ messages = [{" role" : " user" , " content" : " {{ name }} is {{ age }} years old" }],
152
157
response_model = User,
158
+ context = {
159
+ " name" : " Jason" ,
160
+ " age" : 25 ,
161
+ },
153
162
)
154
163
155
164
print (response)
156
- # > name='Jason' age=25
165
+ # > name='Jason' age=25
157
166
158
167
# Using genai's Content type
159
168
response = client.chat.completions.create(
160
169
model = " gemini-2.0-flash-001" ,
161
170
messages = [
162
171
genai.types.Content(
163
172
role = " user" ,
164
- parts = [genai.types.Part.from_text(text = " Jason is 25 years old" )],
173
+ parts = [genai.types.Part.from_text(text = " {{ name }} is {{ age }} years old" )],
165
174
)
166
175
],
167
176
response_model = User,
177
+ context = {
178
+ " name" : " Jason" ,
179
+ " age" : 25 ,
180
+ },
168
181
)
169
182
170
183
print (response)
171
- # > name='Jason' age=25
184
+ # > name='Jason' age=25
172
185
```
173
186
174
187
## Validation and Retries
0 commit comments