Skip to content

Commit 5673494

Browse files
google-genai-botcopybara-github
authored andcommitted
docs: Update code samples to gemini-flash-latest, gemini-3.1-flash-lite-image, and gemini-3.8-flash-tts
PiperOrigin-RevId: 993715348
1 parent 5d38785 commit 5673494

3 files changed

Lines changed: 76 additions & 37 deletions

File tree

‎README.md‎

Lines changed: 67 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ print(response.text)
344344
from google.genai import types
345345

346346
response = client.models.generate_content(
347-
model='gemini-3.1-flash-image',
347+
model='gemini-3.1-flash-lite-image',
348348
contents='A cartoon infographic for flying sneakers',
349349
config=types.GenerateContentConfig(
350350
response_modalities=["IMAGE"],
@@ -360,6 +360,32 @@ for part in response.parts:
360360
generated_image.show()
361361
```
362362

363+
#### with text content input (audio / speech output)
364+
365+
```python
366+
from google.genai import types
367+
368+
response = client.models.generate_content(
369+
model='gemini-3.8-flash-tts',
370+
contents='Give me a cheerful greeting.',
371+
config=types.GenerateContentConfig(
372+
response_modalities=['AUDIO'],
373+
speech_config=types.SpeechConfig(
374+
voice_config=types.VoiceConfig(
375+
prebuilt_voice_config=types.PrebuiltVoiceConfig(
376+
voice_name='Aoede'
377+
)
378+
)
379+
),
380+
),
381+
)
382+
383+
for part in response.parts:
384+
if part.inline_data:
385+
with open('greeting.wav', 'wb') as f:
386+
f.write(part.inline_data.data)
387+
```
388+
363389
#### with uploaded file (Gemini Developer API only)
364390

365391
Download the file in console.
@@ -1566,7 +1592,7 @@ interaction = client.interactions.create(
15661592
model='gemini-flash-latest',
15671593
input='Tell me a short joke about programming.'
15681594
)
1569-
print(interaction.outputs[-1].text)
1595+
print(interaction.output_text)
15701596

15711597
```
15721598

@@ -1580,15 +1606,15 @@ interaction1 = client.interactions.create(
15801606
model='gemini-flash-latest',
15811607
input='Hi, my name is Amir.'
15821608
)
1583-
print(f"Model: {interaction1.outputs[-1].text}")
1609+
print(f"Model: {interaction1.output_text}")
15841610

15851611
# 2. Second turn (passing previous_interaction_id)
15861612
interaction2 = client.interactions.create(
15871613
model='gemini-flash-latest',
15881614
input='What is my name?',
15891615
previous_interaction_id=interaction1.id
15901616
)
1591-
print(f"Model: {interaction2.outputs[-1].text}")
1617+
print(f"Model: {interaction2.output_text}")
15921618

15931619
```
15941620

@@ -1613,7 +1639,7 @@ while True:
16131639
print(f"Status: {interaction.status}")
16141640

16151641
if interaction.status == "completed":
1616-
print("\nFinal Report:\n", interaction.outputs[-1].text)
1642+
print("\nFinal Report:\n", interaction.output_text)
16171643
break
16181644
elif interaction.status in ["failed", "cancelled"]:
16191645
print(f"Failed with status: {interaction.status}")
@@ -1640,7 +1666,7 @@ interaction = client.interactions.create(
16401666
{'type': 'image', 'data': base64_image, 'mime_type': 'image/png'}
16411667
]
16421668
)
1643-
print(interaction.outputs[-1].text)
1669+
print(interaction.output_text)
16441670

16451671
```
16461672

@@ -1675,25 +1701,25 @@ interaction = client.interactions.create(
16751701
)
16761702

16771703
# 3. Handle the tool call
1678-
for output in interaction.outputs:
1679-
if output.type == 'function_call':
1680-
print(f"Tool Call: {output.name}({output.arguments})")
1704+
for step in (interaction.steps or []):
1705+
if step.type == 'function_call':
1706+
print(f"Tool Call: {step.name}({step.arguments})")
16811707

16821708
# Execute your actual function here
1683-
result = get_weather(**output.arguments)
1709+
result = get_weather(**step.arguments)
16841710

16851711
# Send result back to the model
16861712
interaction = client.interactions.create(
16871713
model='gemini-flash-latest',
16881714
previous_interaction_id=interaction.id,
16891715
input=[{
16901716
'type': 'function_result',
1691-
'name': output.name,
1692-
'call_id': output.id,
1717+
'name': step.name,
1718+
'call_id': step.id,
16931719
'result': result
16941720
}]
16951721
)
1696-
print(f"Response: {interaction.outputs[-1].text}")
1722+
print(f"Response: {interaction.output_text}")
16971723

16981724
```
16991725

@@ -1708,11 +1734,7 @@ interaction = client.interactions.create(
17081734
input='Who won the last Super Bowl?',
17091735
tools=[{'type': 'google_search'}]
17101736
)
1711-
1712-
# Find the text output (not the GoogleSearchResultContent)
1713-
text_output = next((o for o in interaction.outputs if o.type == 'text'), None)
1714-
if text_output:
1715-
print(text_output.text)
1737+
print(interaction.output_text)
17161738

17171739
```
17181740

@@ -1724,30 +1746,47 @@ interaction = client.interactions.create(
17241746
input='Calculate the 50th Fibonacci number.',
17251747
tools=[{'type': 'code_execution'}]
17261748
)
1727-
print(interaction.outputs[-1].text)
1749+
print(interaction.output_text)
17281750

17291751
```
17301752

17311753
### Multimodal Output
17321754

1733-
The Interactions API can generate multimodal outputs, such as images. You must specify the `response_modalities`.
1755+
The Interactions API can generate multimodal outputs, such as images or audio. You must specify the `response_modalities`.
1756+
1757+
#### Image Output
17341758

17351759
```python
17361760
import base64
17371761

17381762
interaction = client.interactions.create(
1739-
model='gemini-3-pro-image-preview',
1763+
model='gemini-3.1-flash-lite-image',
17401764
input='Generate an image of a futuristic city.',
17411765
response_modalities=['IMAGE']
17421766
)
17431767

1744-
for output in interaction.outputs:
1745-
if output.type == 'image':
1746-
print(f"Generated image with mime_type: {output.mime_type}")
1747-
# Save the image
1748-
with open("generated_city.png", "wb") as f:
1749-
f.write(base64.b64decode(output.data))
1768+
if interaction.output_image:
1769+
print(f"Generated image with mime_type: {interaction.output_image.mime_type}")
1770+
with open("generated_city.png", "wb") as f:
1771+
f.write(base64.b64decode(interaction.output_image.data))
1772+
1773+
```
1774+
1775+
#### Audio Output
17501776

1777+
```python
1778+
import base64
1779+
1780+
interaction = client.interactions.create(
1781+
model='gemini-3.8-flash-tts',
1782+
input='Say hello and give a one sentence motivational quote.',
1783+
response_modalities=['AUDIO']
1784+
)
1785+
1786+
if interaction.output_audio:
1787+
print(f"Generated audio with mime_type: {interaction.output_audio.mime_type}")
1788+
with open("greeting.wav", "wb") as f:
1789+
f.write(base64.b64decode(interaction.output_audio.data))
17511790
```
17521791

17531792
## Tunings
@@ -2066,7 +2105,7 @@ The structure of the dictionary must match the backend API's request structure.
20662105

20672106
```python
20682107
response = client.models.generate_content(
2069-
model="gemini-2.5-pro",
2108+
model="gemini-flash-latest",
20702109
contents="What is the weather in Boston? and how about Sunnyvale?",
20712110
config=types.GenerateContentConfig(
20722111
tools=[get_current_weather],

‎google/genai/chats.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def send_message(
237237
238238
.. code-block:: python
239239
240-
chat = client.chats.create(model='gemini-2.0-flash')
240+
chat = client.chats.create(model='gemini-flash-latest')
241241
response = chat.send_message('tell me a story')
242242
"""
243243

@@ -401,7 +401,7 @@ def send_message_stream(
401401
402402
.. code-block:: python
403403
404-
chat = client.chats.create(model='gemini-2.0-flash')
404+
chat = client.chats.create(model='gemini-flash-latest')
405405
for chunk in chat.send_message_stream('tell me a story'):
406406
print(chunk.text)
407407
"""
@@ -632,7 +632,7 @@ async def send_message(
632632
633633
.. code-block:: python
634634
635-
chat = client.aio.chats.create(model='gemini-2.0-flash')
635+
chat = client.aio.chats.create(model='gemini-flash-latest')
636636
response = await chat.send_message('tell me a story')
637637
"""
638638
method_config = config if config else self._config
@@ -879,7 +879,7 @@ async def send_message_stream(
879879
880880
.. code-block:: python
881881
882-
chat = client.aio.chats.create(model='gemini-2.0-flash')
882+
chat = client.aio.chats.create(model='gemini-flash-latest')
883883
async for chunk in await chat.send_message_stream('tell me a story'):
884884
print(chunk.text)
885885
"""

‎google/genai/client.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,11 @@ async def aclose(self) -> None:
245245
enterprise=True, project='my-project-id', location='us-central1'
246246
).aio
247247
response_1 = await async_client.models.generate_content(
248-
model='gemini-2.0-flash',
248+
model='gemini-flash-latest',
249249
contents='Hello World',
250250
)
251251
response_2 = await async_client.models.generate_content(
252-
model='gemini-2.0-flash',
252+
model='gemini-flash-latest',
253253
contents='Hello World',
254254
)
255255
# Close the client to release resources.
@@ -638,14 +638,14 @@ def close(self) -> None:
638638
from google.genai import Client
639639
640640
client = Client(
641-
vertexai=True, project='my-project-id', location='us-central1'
641+
enterprise=True, project='my-project-id', location='us-central1'
642642
)
643643
response_1 = client.models.generate_content(
644-
model='gemini-2.0-flash',
644+
model='gemini-flash-latest',
645645
contents='Hello World',
646646
)
647647
response_2 = client.models.generate_content(
648-
model='gemini-2.0-flash',
648+
model='gemini-flash-latest',
649649
contents='Hello World',
650650
)
651651
# Close the client to release resources.

0 commit comments

Comments
 (0)