Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 23, 2025

The 04-semantic-kernel-tool.ipynb notebook was designed to display function call details in a collapsible section when the agent invokes tools, but this functionality was not working due to a limitation in the Semantic Kernel Python streaming API.

Problem

The notebook code was correctly written to detect FunctionCallContent and FunctionResultContent in streaming responses, but the Semantic Kernel Python streaming API (version 1.35.0) was only yielding FunctionCallContent and StreamingTextContent, not FunctionResultContent. This meant that while function calls were working correctly, users couldn't see the function call details that were supposed to appear in the "Function Calls" collapsible section.

As shown in the issue screenshot, the agent successfully calls functions like get_destinations() and get_availability() and provides correct responses, but the function call transparency features were missing.

Root Cause

This was a known upstream issue in microsoft/semantic-kernel that was fixed in PR #9974 (merged December 18, 2024). The issue affected the streaming API where FunctionResultContent was not being yielded during streaming function calls.

Solution

Implemented a backward-compatible workaround that:

  1. Tracks function calls immediately when FunctionCallContent is received during streaming
  2. Maintains compatibility with newer semantic-kernel versions that properly yield FunctionResultContent
  3. Provides fallback logic to display function call details when FunctionResultContent is not available in streaming
  4. Shows user-friendly messages indicating successful function execution

Key Changes

# Added tracking for detected function calls
function_calls_made = []  # Track function calls that were detected

# Capture function calls immediately when FunctionCallContent is received
if current_function_name and argument_buffer:
    formatted_args = argument_buffer.strip()
    try:
        parsed_args = json.loads(formatted_args)
        formatted_args = json.dumps(parsed_args)
    except Exception:
        pass  # leave as raw string

    function_calls_made.append({
        'name': current_function_name,
        'args': formatted_args
    })

# Fallback logic when FunctionResultContent is not streamed
if function_calls_made and not function_calls:
    for func_call in function_calls_made:
        function_calls.append(f"Calling function: {func_call['name']}({func_call['args']})")
    
    if len(function_calls_made) > 0:
        function_calls.append("\nFunction results were processed successfully (results used to generate the response above)")

Expected Behavior After Fix

When users run the notebook, they will now see:

  • Function call details in the collapsible "Function Calls" section
  • Clear indication of which functions were called with what arguments
  • Confirmation that function results were processed successfully
  • Maintained streaming text output as before

This restores the intended transparency and debugging capabilities for tool use demonstrations.

Fixes #194.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] A code sample of 04-tool-use doesn't work as intended Fix streaming function call display in 04-semantic-kernel-tool.ipynb Jul 23, 2025
@Copilot Copilot AI requested a review from koreyspace July 23, 2025 08:41
Copilot finished work on behalf of koreyspace July 23, 2025 08:41
@doctornick42
Copy link

Hi @koreyspace. I think the same problem also exists in 02-explore-agentic-frameworks/code_samples/02-semantic-kernel.ipynb

@leestott leestott requested a review from Copilot September 9, 2025 20:42
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Fixes streaming function call display in the Semantic Kernel tool notebook by implementing a backward-compatible workaround for a limitation in the streaming API that prevented function call details from being shown to users.

  • Adds immediate tracking of function calls when FunctionCallContent is received during streaming
  • Implements fallback logic to display function call details when FunctionResultContent is not available
  • Provides user-friendly confirmation messages for successful function execution
Comments suppressed due to low confidence (2)

04-tool-use/code_samples/04-semantic-kernel-tool.ipynb:1

  • This logic captures function calls on every FunctionCallContent chunk, potentially creating duplicate entries in function_calls_made for the same function call. Consider adding a check to prevent duplicates or only capture when the function call is complete.
{

04-tool-use/code_samples/04-semantic-kernel-tool.ipynb:1

  • The condition if len(function_calls_made) > 0: on line 294 is redundant since it's already inside a block that checks if function_calls_made (which evaluates to False for empty lists). This check can be removed.
{

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@leestott leestott removed the request for review from koreyspace September 9, 2025 20:43
@leestott leestott marked this pull request as ready for review September 9, 2025 20:43
@leestott leestott merged commit 012eaf6 into main Sep 9, 2025
4 checks passed
@leestott leestott deleted the copilot/fix-194 branch September 9, 2025 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A code sample of 04-tool-use doesn't work as intended
4 participants