Skip to content

Commit ba031e6

Browse files
authored
Python: Properly handle AzureAI Search results for AzureAIAgent (#12654)
### Motivation and Context After the Azure AI Agent service went to GA, there were some updates in the underlying SDK. When there are azure_ai_search results while running the agent, they no longer show up as attributes related to the tool call. They are present in the dictionary, so our current code is failing when trying to access the attribute. This PR updates how we form the FunctionCallContent or FunctionResultContent for both azure_ai_search streaming and non-streaming. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description Update how we form the FunctionCallContent or FunctionResultContent for both azure_ai_search streaming and non-streaming for the AzureAIAgent. - Closes #12422 <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
1 parent 9f33c90 commit ba031e6

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

python/semantic_kernel/agents/azure_ai/agent_content_generation.py

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -299,26 +299,33 @@ def generate_bing_grounding_content(
299299
@experimental
300300
def generate_azure_ai_search_content(
301301
agent_name: str, azure_ai_search_tool_call: "RunStepAzureAISearchToolCall"
302-
) -> ChatMessageContent:
302+
) -> ChatMessageContent | None:
303303
"""Generate function result content related to an Azure AI Search Tool."""
304-
message_content: ChatMessageContent = ChatMessageContent(role=AuthorRole.ASSISTANT, name=agent_name) # type: ignore
304+
items: list[FunctionCallContent | FunctionResultContent] = []
305+
305306
# Azure AI Search tool call contains both tool call input and output
306-
message_content.items.append(
307-
FunctionCallContent(
308-
id=azure_ai_search_tool_call.id,
309-
name=azure_ai_search_tool_call.type,
310-
function_name=azure_ai_search_tool_call.type,
311-
arguments=azure_ai_search_tool_call.azure_ai_search.get("input"),
307+
arguments = azure_ai_search_tool_call.azure_ai_search.get("input")
308+
if arguments:
309+
items.append(
310+
FunctionCallContent(
311+
id=azure_ai_search_tool_call.id,
312+
name=azure_ai_search_tool_call.type,
313+
function_name=azure_ai_search_tool_call.type,
314+
arguments=arguments,
315+
inner_content=azure_ai_search_tool_call,
316+
)
312317
)
313-
)
314-
message_content.items.append(
315-
FunctionResultContent(
316-
function_name=azure_ai_search_tool_call.type,
317-
id=azure_ai_search_tool_call.id,
318-
result=azure_ai_search_tool_call.azure_ai_search.get("output"),
318+
result = azure_ai_search_tool_call.azure_ai_search.get("output")
319+
if result:
320+
items.append(
321+
FunctionResultContent(
322+
function_name=azure_ai_search_tool_call.type,
323+
id=azure_ai_search_tool_call.id,
324+
result=result,
325+
inner_content=azure_ai_search_tool_call,
326+
)
319327
)
320-
)
321-
return message_content
328+
return ChatMessageContent(role=AuthorRole.ASSISTANT, name=agent_name, items=items) if items else None # type: ignore
322329

323330

324331
@experimental
@@ -506,29 +513,39 @@ def generate_streaming_azure_ai_search_content(
506513
for index, tool in enumerate(step_details.tool_calls):
507514
if tool.type == "azure_ai_search":
508515
azure_ai_search_tool = cast(RunStepAzureAISearchToolCall, tool)
509-
arguments = getattr(azure_ai_search_tool, "azure_ai_search", None)
510-
items.append(
511-
FunctionCallContent(
512-
id=azure_ai_search_tool.id,
513-
index=index,
514-
name=azure_ai_search_tool.type,
515-
function_name=azure_ai_search_tool.type,
516-
arguments=arguments,
516+
azure_ai_search_dict: dict = azure_ai_search_tool.get("azure_ai_search", None)
517+
arguments = azure_ai_search_dict.get("input", {}) if azure_ai_search_dict else None
518+
if arguments:
519+
items.append(
520+
FunctionCallContent(
521+
id=azure_ai_search_tool.id,
522+
index=index,
523+
name=azure_ai_search_tool.type,
524+
function_name=azure_ai_search_tool.type,
525+
arguments=arguments,
526+
inner_content=azure_ai_search_tool,
527+
)
517528
)
518-
)
519-
items.append(
520-
FunctionResultContent(
521-
function_name=azure_ai_search_tool.type,
522-
id=azure_ai_search_tool.id,
523-
result=azure_ai_search_tool.azure_ai_search.get("output"),
529+
result = azure_ai_search_dict.get("output", {}) if azure_ai_search_dict else None
530+
if result:
531+
items.append(
532+
FunctionResultContent(
533+
function_name=azure_ai_search_tool.type,
534+
id=azure_ai_search_tool.id,
535+
result=result,
536+
inner_content=azure_ai_search_tool,
537+
)
524538
)
525-
)
526539

527-
return StreamingChatMessageContent(
528-
role=AuthorRole.ASSISTANT,
529-
name=agent_name,
530-
choice_index=0,
531-
items=items, # type: ignore
540+
return (
541+
StreamingChatMessageContent(
542+
role=AuthorRole.ASSISTANT,
543+
name=agent_name,
544+
choice_index=0,
545+
items=items, # type: ignore
546+
)
547+
if items
548+
else None
532549
) # type: ignore
533550

534551

0 commit comments

Comments
 (0)