Skip to content

Commit 2920145

Browse files
wukathcopybara-github
authored andcommitted
fix: Enable history_config on Vertex AI now that it is supported
Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 936823570
1 parent ccb8138 commit 2920145

2 files changed

Lines changed: 10 additions & 81 deletions

File tree

src/google/adk/flows/llm_flows/base_llm_flow.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -578,18 +578,9 @@ async def run_live(
578578
# initial_history_in_client_content to True. This tells the Live server
579579
# that the provided history already includes the model's past responses,
580580
# preventing the server from generating duplicate responses for those replayed turns.
581-
#
582-
# ``history_config`` is only supported by the Gemini Developer API
583-
# backend; the Vertex AI / Gemini Enterprise Agent Platform backend has
584-
# no such field on its live setup message and rejects it. On Vertex,
585-
# history is instead seeded by ``send_history`` below
586-
# (``send_client_content`` with prior turns), so we skip
587-
# ``history_config`` for that backend.
588581
if (
589582
llm_request.contents
590583
and not invocation_context.live_session_resumption_handle
591-
and isinstance(llm, Gemini)
592-
and llm._api_backend == GoogleLLMVariant.GEMINI_API # pylint: disable=protected-access
593584
):
594585
if not llm_request.live_connect_config:
595586
llm_request.live_connect_config = types.LiveConnectConfig()

tests/unittests/flows/llm_flows/test_base_llm_flow.py

Lines changed: 10 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,8 +1487,15 @@ async def mock_receive_2():
14871487

14881488

14891489
@pytest.mark.asyncio
1490-
async def test_run_live_history_config_set_for_gemini_api_backend():
1491-
"""history_config is auto-set when seeding history on the Gemini API backend."""
1490+
@pytest.mark.parametrize(
1491+
'api_backend',
1492+
[
1493+
GoogleLLMVariant.GEMINI_API,
1494+
GoogleLLMVariant.VERTEX_AI,
1495+
],
1496+
)
1497+
async def test_run_live_history_config_set_for_all_backends(api_backend):
1498+
"""Test that run_live sets history_config for all backends."""
14921499

14931500
real_model = Gemini(model='gemini-3.1-flash-live-preview')
14941501
mock_connection = mock.AsyncMock()
@@ -1535,7 +1542,7 @@ async def mock_receive():
15351542
Gemini,
15361543
'_api_backend',
15371544
new_callable=mock.PropertyMock,
1538-
return_value=GoogleLLMVariant.GEMINI_API,
1545+
return_value=api_backend,
15391546
):
15401547
try:
15411548
async for _ in flow.run_live(invocation_context):
@@ -1553,75 +1560,6 @@ async def mock_receive():
15531560
)
15541561

15551562

1556-
@pytest.mark.asyncio
1557-
async def test_run_live_history_config_not_set_for_vertex_backend():
1558-
"""history_config is NOT auto-set on the Vertex backend (it rejects it).
1559-
1560-
The Vertex AI / Gemini Enterprise Agent Platform live setup message has no
1561-
``history``/``history_config`` field. ADK seeds Vertex history via
1562-
``send_history`` (``send_client_content``) instead, so the auto-injection of
1563-
``history_config`` must be skipped for this backend.
1564-
"""
1565-
1566-
real_model = Gemini(model='gemini-3.1-flash-live-preview')
1567-
mock_connection = mock.AsyncMock()
1568-
1569-
class StopTestError(Exception):
1570-
pass
1571-
1572-
async def mock_receive():
1573-
yield LlmResponse(
1574-
content=types.Content(parts=[types.Part.from_text(text='hi')])
1575-
)
1576-
raise StopTestError('stop')
1577-
1578-
mock_connection.receive = mock.Mock(side_effect=mock_receive)
1579-
1580-
agent = Agent(name='test_agent', model=real_model)
1581-
invocation_context = await testing_utils.create_invocation_context(
1582-
agent=agent
1583-
)
1584-
invocation_context.live_request_queue = LiveRequestQueue()
1585-
1586-
flow = BaseLlmFlowForTesting()
1587-
1588-
with mock.patch.object(flow, '_send_to_model', new_callable=AsyncMock):
1589-
1590-
async def mock_preprocess(ctx, req):
1591-
req.contents = [
1592-
types.Content(parts=[types.Part.from_text(text='history')])
1593-
]
1594-
yield Event(id=Event.new_id(), author='test')
1595-
1596-
with mock.patch.object(
1597-
flow, '_preprocess_async', side_effect=mock_preprocess
1598-
):
1599-
with mock.patch.object(
1600-
Gemini, '_api_backend', new_callable=mock.PropertyMock
1601-
) as mock_backend:
1602-
mock_backend.return_value = GoogleLLMVariant.VERTEX_AI
1603-
with mock.patch(
1604-
'google.adk.models.google_llm.Gemini.connect'
1605-
) as mock_connect:
1606-
mock_connect.return_value.__aenter__.return_value = mock_connection
1607-
1608-
try:
1609-
async for _ in flow.run_live(invocation_context):
1610-
pass
1611-
except StopTestError:
1612-
pass
1613-
1614-
assert mock_connect.call_count == 1
1615-
called_req = mock_connect.call_args[0][0]
1616-
# history_config must NOT be auto-injected on Vertex.
1617-
assert (
1618-
called_req.live_connect_config is None
1619-
or called_req.live_connect_config.history_config is None
1620-
)
1621-
# History is still seeded via send_history (send_client_content).
1622-
mock_connection.send_history.assert_awaited_once()
1623-
1624-
16251563
@pytest.mark.asyncio
16261564
async def test_run_live_respects_explicit_initial_history_in_client_content_false():
16271565
"""Test that run_live respects explicit initial_history_in_client_content=False in RunConfig."""

0 commit comments

Comments
 (0)