Skip to content

How to get RunContextWrapper fields from Run.run_streamed ? #453

Closed
@muneebarshadd

Description

@muneebarshadd

Question

I created an agent with hooks that updates the context field:

agent= Agent[MyContext]( name="agent name", instructions="some instructions", hooks=AgentHooks(), )

These are my hooks:

class MyContext(BaseModel):
    queryConverted: bool = False
    greetings: bool = False


class AgentHooks(RunHooks):


    async def on_start(
        self, context: RunContextWrapper[MyContext], agent: Agent[MyContext]
    ) -> None:
        """Called before the agent is invoked. Called each time the current agent changes."""
        pass

    async def on_end(
        self,
        context: RunContextWrapper[MyContext],
        agent: Agent[MyContext],
        output: Any,
    ) -> None:
        """Called when the agent produces a final output."""
        context.context.queryConverted= True
        context.context.greetings= False


I am running the agent like this:

result = Runner.run_streamed(agent, input="This is my query?", context=MyContext())

I am streaming the events using this line of code:

async for event in result.stream_events()

and I am trying to access the MyContext fields from the output. Is there a way to do this? Basically I am looking for a way to get the context when I run the agent. The examples on github just print the context fields but I dont want to do that

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions