Hello There,
We’re having issues when attempting multi-turn AI interaction.
The issues happen when the interaction run in ‘backbround=True’ following by the retrieved interaction in “stream=True”.
The first interaction work fine, however when trying to feed the environement_id and the previous_interaction_id returned from the first original interaction to a second call, the api crash from a BadRequestError : Error code: 400 - {‘error’: {‘message’: ‘Precondition check failed.’, ‘code’: ‘invalid_request’}}.
if we feed the second interaction with “remote” as environement, the call go through as expected, but this create a whole new fresh environements.
import os, time
from google import genai
os.environ["GEMINI_API_KEY"] = ""
client = genai.Client()
def handleInteraction(prompt: str, previous_interaction = None):
agents = "antigravity-preview-05-2026"
environment = previous_interaction.environment_id if previous_interaction else "remote"
previous_interaction_id = previous_interaction.id if previous_interaction else None
print(f"prompt : {prompt}")
interaction = client.interactions.create(
agent=agents,
input=prompt,
environment= environment,
previous_interaction_id = previous_interaction_id,
background=True
)
last_event_id = None
while True:
try:
# Retrieve the stream. If resuming, pass last_event_id
stream = client.interactions.get(
id=interaction.id,
stream=True,
last_event_id=last_event_id
)
for event in stream:
# Log event updates and capture event_id if present
if event.event_id:
last_event_id = event.event_id
if event.event_type == "step.delta" and event.delta.type == "text":
print(event.delta.text, end="", flush=True)
if event.event_type == "interaction.completed":
return interaction
except Exception as e:
print(f"\n[Connection lost: {e}. Reconnecting in 3s...]")
time.sleep(3)
prev_interaction = handleInteraction("say hello")
prev_interaction = handleInteraction("say hello again", prev_interaction)
although, I’m unsure about this whole “new fresh environments”.
if we take the exemple scripts from https://ai.google.dev/gemini-api/docs/background-execution about multi-turn conversation. The exemple tell us to use the “remote” keyword for the environement in both the first and second interaction, which don’t make much sense to me if we use the “remote” keyword and not providing an id, it because we want to get a whole new fresh environement right ? so how could the AI find the file created in the first interaction.
So I took the liberty of running some test, and when settings the second interaction to “remote” with the previous interaction ID, the AI still manage to list the file, which mean it did use the original environment from the first interaction.
Yet if we try to list the environements after running both interaction using
client.environments.list(page_size=5):
We can see 2 environments have been created, not one. yet the AI from the second interaction still managed to find the file from the first interaction.
so what happened ? did a new environments got created, but was then completely ignored by the ‘previous interaction id’ data which decided to use the first environement ?
because of this confusion, i’m unsure if I’m supposed to keep using the “remote” keyword and therefor solving my problem, or if there is indeed a google-side bug which prevent me from feeding the id of the environement I wish to use.
Thanks for the help.