ai-agents-for-beginners icon indicating copy to clipboard operation
ai-agents-for-beginners copied to clipboard

history has value that doesn't support encoding.

Open saisiva249 opened this issue 5 months ago • 7 comments

when running the notebook I am facing this issue

history has value that doesn't support encoding.

Function failed. Error: Argument 'history' has a value that doesn't support automatic encoding. Set allow_dangerously_set_content to 'True' for this argument and implement custom encoding, or provide the value as a string. Kernel Function Selection Strategy next method failed Traceback (most recent call last): File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/agents/strategies/selection/kernel_function_selection_strategy.py", line 95, in select_agent result = await self.function.invoke(kernel=self.kernel, arguments=arguments) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/functions/kernel_function.py", line 290, in invoke raise e File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/functions/kernel_function.py", line 275, in invoke await stack(function_context) File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/functions/kernel_function_from_prompt.py", line 172, in _invoke_internal prompt_render_result = await self._render_prompt(context) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/functions/kernel_function_from_prompt.py", line 285, in _render_prompt await stack(prompt_render_context) File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/functions/kernel_function_from_prompt.py", line 303, in _inner_render_prompt context.rendered_prompt = await self.prompt_template.render(context.kernel, context.arguments) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/prompt_template/kernel_prompt_template.py", line 94, in render return await self.render_blocks(self._blocks, kernel, arguments) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/prompt_template/kernel_prompt_template.py", line 115, in render_blocks arguments = self._get_trusted_arguments(arguments or KernelArguments()) File "/home/vscode/.local/lib/python3.13/site-packages/semantic_kernel/prompt_template/prompt_template_base.py", line 47, in _get_trusted_arguments new_args[name] = self._get_encoded_value_or_default(name, value) ... ...<3 lines>... ) NotImplementedError: Argument 'history' has a value that doesn't support automatic encoding. Set allow_dangerously_set_content to 'True' for this argument and implement custom encoding, or provide the value as a string. Failed to select agent: Agent Failure - Strategy failed to execute function.

saisiva249 avatar Sep 14 '25 16:09 saisiva249

👋 Thanks for contributing @saisiva249! We will review the issue and get back to you soon.

github-actions[bot] avatar Sep 14 '25 16:09 github-actions[bot]

facing the same problem while trying to follow the doc: https://learn.microsoft.com/en-us/semantic-kernel/support/archive/agent-chat-example?pivots=programming-language-python

Error: "Function failed. Error: Argument 'lastmessage' has a value that doesn't support automatic encoding. Set allow_dangerously_set_content to 'True' for this argument and implement custom encoding, or provide the value as a string. Error during chat invocation: Argument 'lastmessage' has a value that doesn't support automatic encoding. Set allow_dangerously_set_content to 'True' for this argument and implement custom encoding, or provide the value as a string."

shaswatapal avatar Sep 18 '25 06:09 shaswatapal

Have the same - even when following this: https://learn.microsoft.com/en-us/semantic-kernel/support/archive/agent-chat-example?pivots=programming-language-python

Using the gpt-4.1 model.

rmoreirao avatar Sep 22 '25 18:09 rmoreirao

I remove this error by commenting the arguments:

  agent = ChatCompletionAgent(
            service=self.client,
            name=WeatherForecastAgent.agent_name,
            instructions=WeatherForecastAgent.agent_instructions,
            kernel=kernel#,
            # arguments=KernelArguments(
            #     chat_history=ChatHistory(),
            #     settings=self.execution_settings,
            #     kernel=kernel,
            #  )
        )

But i don"t undertand the impact to remove them....

I'm using the MemoryStorage for the state, but i'm not able to keep the history of the conversation ! STORAGE = MemoryStorage()


@AGENT_APP.activity("message")
async def on_message(context: TurnContext, state: TurnState):
    # context.streaming_response.queue_informative_update(
    #     "Working on a response for you..."
    # )

    chat_history = state.get_value(
        "ConversationState.chatHistory", lambda: ChatHistory(), target_cls=ChatHistory
    )

    # Print all information about the context userid, conversationid, etc.
    print(f"Context info: {context.activity}")
    print(f"User ID: {context.activity.from_property.id}")
    print(f"Conversation ID: {context.activity.conversation.id}")
    print(f"Service URL: {context.activity.service_url}")
    print(f"Channel ID: {context.activity.channel_id}")
    print(f"Recipient ID: {context.activity.recipient.id}")
    print(f"Locale: {context.activity.locale}")
    print(f"Timestamp: {context.activity.timestamp}")
    print(f"Type: {context.activity.type}")
    print(f"ID: {context.activity.id}")
    print(f"Reply to ID: {context.activity.reply_to_id}")
    print(f"Name: {context.activity.name}")

    # Debug: Check the current content of chat_history
    print(f"Number of messages in history BEFORE: {len(chat_history.messages)}")
    print(f"Existing messages: {[msg.content for msg in chat_history.messages]}")

    print(f"User message: {context.activity.text}")

    # Add the user's message to the chat history
    chat_history.add_user_message(context.activity.text)

    # Create the bot's response
    bot_response = f"you said: {context.activity.text}"

    # Add the bot's response to the chat history
    chat_history.add_assistant_message(bot_response)

    # Debug: Check the current content of chat_history
    print(f"Number of messages in history AFTER: {len(chat_history.messages)}")
    print(f"All messages: {[msg.content for msg in chat_history.messages]}")

    # IMPORTANT: Save the updated history back to state
    state.set_value("ConversationState.chatHistory", chat_history)

    # Check that the save was successful
    saved_history = state.get_value("ConversationState.chatHistory", lambda: ChatHistory(), target_cls=ChatHistory)
    print(f"Messages saved: {len(saved_history.messages)}")

    await context.send_activity(bot_response)

=> History messages always empty :(

didaskein avatar Sep 22 '25 20:09 didaskein

I find the way to have the history messages in the state :


@AGENT_APP.activity("message")
async def on_message(context: TurnContext, state: TurnState):

    **chat_history = state.get_value(
        "ConversationState.chatHistory", lambda: ChatHistory(), target_cls=ChatHistory
    )**

    print(f"BEFORE: Number of messages in history : {len(chat_history)}")
    print(f"BEFORE: All messages: {[msg for msg in chat_history]}")

    # Print all information about the context userid, conversationid, etc.
    print(f"Chat history object ID: {id(chat_history)}")
    print(f"User ID: {context.activity.from_property.id}")
    print(f"Conversation ID: {context.activity.conversation.id}")
    print(f"User name : {context.activity.from_property.name}")
    print(f"AAD Object ID: {context.activity.from_property.aad_object_id}")

    bot_response = f"you said: {context.activity.text}"

    **chat_history.add_user_message(context.activity.text)
    chat_history.add_assistant_message(bot_response)**

    # Save the modified history to state & Save the state to storage
    **state.set_value("ConversationState.chatHistory", chat_history)
    await state.save(context)**

    await context.send_activity(bot_response)

didaskein avatar Sep 23 '25 19:09 didaskein

Thanks for reporitng. @didaskein would you like to make a PR for this?

koreyspace avatar Oct 15 '25 11:10 koreyspace

I'm sorry but I've been trying to fix this but I still can't get it to work. Is there anyone who was able to resolve this issue?

kaikuzai avatar Oct 21 '25 01:10 kaikuzai