@@ -2028,3 +2028,122 @@ async def test_generate_content_via_interactions_non_streaming_yields_single_res
20282028 assert len (responses ) == 1
20292029 assert responses [0 ].interaction_id == 'interaction_ns'
20302030 assert responses [0 ].content .parts [0 ].text == 'Sunny in Tokyo.'
2031+
2032+
2033+ def _build_stream_with_environment () -> list [object ]:
2034+ """A streamed interaction whose completed event carries an environment id."""
2035+ now = datetime .now (timezone .utc ).isoformat ()
2036+ created = InteractionCreatedEvent (
2037+ event_type = 'interaction.created' ,
2038+ interaction = InteractionSseEventInteraction (
2039+ id = 'interaction_env' ,
2040+ created = now ,
2041+ updated = now ,
2042+ status = 'requires_action' ,
2043+ steps = [],
2044+ ),
2045+ )
2046+ step_start = StepStart (
2047+ event_type = 'step.start' ,
2048+ index = 0 ,
2049+ step = ModelOutputStep (type = 'model_output' ),
2050+ )
2051+ step_delta = StepDelta (
2052+ event_type = 'step.delta' ,
2053+ index = 0 ,
2054+ delta = {'type' : 'text' , 'text' : 'hi' },
2055+ )
2056+ step_stop = StepStop (event_type = 'step.stop' , index = 0 )
2057+ completed = InteractionCompletedEvent (
2058+ event_type = 'interaction.completed' ,
2059+ interaction = InteractionSseEventInteraction (
2060+ id = 'interaction_env' ,
2061+ created = now ,
2062+ updated = now ,
2063+ status = 'completed' ,
2064+ environment_id = 'env_xyz' ,
2065+ steps = [
2066+ ModelOutputStep (
2067+ type = 'model_output' ,
2068+ content = [TextContent (type = 'text' , text = 'hi' )],
2069+ )
2070+ ],
2071+ ),
2072+ )
2073+ return [created , step_start , step_delta , step_stop , completed ]
2074+
2075+
2076+ def test_create_interactions_surfaces_environment_id ():
2077+ api_client = _FakeApiClient (_build_stream_with_environment ())
2078+
2079+ async def _collect ():
2080+ out = []
2081+ async for r in interactions_utils ._create_interactions (
2082+ api_client ,
2083+ create_kwargs = {'agent' : 'agents/a' , 'input' : []},
2084+ stream = True ,
2085+ ):
2086+ out .append (r )
2087+ return out
2088+
2089+ responses = asyncio .run (_collect ())
2090+ assert responses , 'expected streamed responses'
2091+ # The env id arrives only on the completed event, so earlier partial
2092+ # responses carry no environment id.
2093+ assert responses [0 ].environment_id is None
2094+ assert responses [- 1 ].environment_id == 'env_xyz'
2095+
2096+
2097+ class _FakeNonStreamInteractions :
2098+ """Fake interactions resource returning a full Interaction (non-streaming)."""
2099+
2100+ def __init__ (self , interaction : Interaction ):
2101+ self ._interaction = interaction
2102+
2103+ async def create (self , ** _kwargs ):
2104+ return self ._interaction
2105+
2106+
2107+ class _FakeNonStreamAio :
2108+ """Namespace matching the expected api_client.aio shape (non-streaming)."""
2109+
2110+ def __init__ (self , interaction : Interaction ):
2111+ self .interactions = _FakeNonStreamInteractions (interaction )
2112+
2113+
2114+ class _FakeNonStreamApiClient :
2115+ """Minimal fake API client whose create() returns a full Interaction."""
2116+
2117+ def __init__ (self , interaction : Interaction ):
2118+ self .aio = _FakeNonStreamAio (interaction )
2119+
2120+
2121+ def test_create_interactions_surfaces_environment_id_non_stream ():
2122+ interaction = Interaction (
2123+ id = 'interaction_ns' ,
2124+ status = 'completed' ,
2125+ created = datetime .now (timezone .utc ).isoformat (),
2126+ updated = datetime .now (timezone .utc ).isoformat (),
2127+ environment_id = 'env_ns' ,
2128+ steps = [
2129+ ModelOutputStep (
2130+ type = 'model_output' ,
2131+ content = [TextContent (type = 'text' , text = 'hi' )],
2132+ )
2133+ ],
2134+ )
2135+ api_client = _FakeNonStreamApiClient (interaction )
2136+
2137+ async def _collect ():
2138+ out = []
2139+ async for r in interactions_utils ._create_interactions (
2140+ api_client ,
2141+ create_kwargs = {'agent' : 'agents/a' , 'input' : []},
2142+ stream = False ,
2143+ ):
2144+ out .append (r )
2145+ return out
2146+
2147+ responses = asyncio .run (_collect ())
2148+ assert len (responses ) == 1
2149+ assert responses [- 1 ].environment_id == 'env_ns'
0 commit comments