Skip to content

Fix/remove redundant session state validation #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ren8k
Copy link

@ren8k ren8k commented Apr 30, 2025

Inline Agent SDK: Remove session state validation checks to enable manual RoC processing workflows

Instructions

  • Do not erase any parts of this template that are not applicable to your pull request.
  • If a section is not applicable, explicitly state the reason.
    • Tick the checkboxes for the items you have completed.
  • These are mandatory requirements, not mere suggestions.

Describe your changes

  • Concise description of the PR
Changes to InlineAgent.invoke method by removing validation checks that block valid session state usage patterns. This change allows for manual processing of Return of Control events when using process_response=False, enabling legitimate asynchronous workflows with the library.

Issue ticket number and link

  • Issue # (if applicable)

Not applicable. This change addresses an API inconsistency that prevents a valid use case.


All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Are you uploading a dataset?
  • Have you added contributions to RELEASE NOTES?

New Example Submissions:

  • Have you tested your code, and made sure the functionality runs successfully? Provide screenshots.
  • Have you linted your Python code with black?
  • Does this implementation use the shared tools src/utils/*. List them here:
1. web_search
2. ...
  • Does this implementation use the helper functions src/utils/*. List them here:
1. bedrock_agent_helper.py
2. ...

src/utils Submissions:

Changes to the utils folder won't be accepted. Instead, open a new issue.


src/shared tool Submissions:

Changes to existing tools won't be accepted. Instead, open a new issue.

  • Business justification for including a new tool
This tool is necessary because ...
  • How is this tool implemented?
      • AWS CDK
      • AWS CloudFormation (recommended)


Technical Details

The PR removes the following validation checks from inline_agent.py:

if "returnControlInvocationResults" in session_state:
    raise ValueError(
        "returnControlInvocationResults key is not supported in inlineSessionState"
    )

if "invocationId" in session_state:
    raise ValueError("invocationId key is not supported in inlineSessionState")

Rationale

  • The invoke method supports a process_response=False parameter which returns the raw response for manual processing.
  • When manually processing events, it's common to handle Return of Control (RoC) events using ProcessROC.process_roc.
  • The output of process_roc includes session state information needed for subsequent calls.
  • The current validation rejects session state that comes from process_roc, breaking this valid workflow.
  • Since the library already supports this pattern with its API design, these validation checks contradict that intent.

This change enables proper manual event processing workflows while still maintaining the library's intended functionality.

Sample code

import asyncio

from InlineAgent.action_group import ActionGroup
from InlineAgent.agent import InlineAgent
from InlineAgent.agent.process_roc import ProcessROC


# Step 1: Define tools with Docstring
def get_current_weather(location: str, state: str, unit: str = "fahrenheit") -> dict:
    """
    Get the current weather in a given location.

    Parameters:
        location: The city, e.g., San Francisco
        state: The state eg CA
        unit: The unit to use, e.g., fahrenheit or celsius. Defaults to "fahrenheit"
    """
    return "Weather is 70 fahrenheit"


# Step 2: Logically group tools together
weather_action_group = ActionGroup(
    name="WeatherActionGroup",
    description="This is action group to get weather",
    tools=[get_current_weather],
)

# Step 3: Define agent
agent = InlineAgent(
    foundation_model="us.anthropic.claude-3-5-haiku-20241022-v1:0",
    instruction="You are a friendly assistant that is responsible for getting the current weather.",
    action_groups=[weather_action_group],
    agent_name="MockAgent",
)


# Step 4: Invoke agent
async def main():
    agent_answer = ""
    session_id = "123"
    session_state = {}

    while not agent_answer:
        response = await agent.invoke(
            input_text="What is the weather of NY? ",
            session_id=session_id,
            session_state=session_state,
            process_response=False,
        )

        event_stream = response["completion"]
        for event in event_stream:
            print("=== Event Stream ===")
            print(event)
            print("========================")
            if "returnControl" in event:
                session_state = await ProcessROC.process_roc(
                    inlineSessionState={},
                    roc_event=event["returnControl"],
                    tool_map=agent.tool_map,
                )
                print("=== Session State ===")
                print(session_state)
                print("========================")
            # Get Final Answer
            if "chunk" in event:
                agent_answer = event["chunk"]["bytes"].decode("utf8")
                print("=== Agent Answer ===")
                print(agent_answer)
                print("========================")
                break

    return agent_answer


if __name__ == "__main__":
    result = asyncio.run(main())
    print("\nFinal Answer:", result)

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.

1 participant