MCPcopy
hub / github.com/openai/openai-agents-python / run

Method run

src/agents/run.py:450–1562  ·  view source on GitHub ↗
(
        self,
        starting_agent: Agent[TContext],
        input: str | list[TResponseInputItem] | RunState[TContext],
        **kwargs: Unpack[RunOptions[TContext]],
    )

Source from the content-addressed store, hash-verified

448 """
449
450 async def run(
451 self,
452 starting_agent: Agent[TContext],
453 input: str | list[TResponseInputItem] | RunState[TContext],
454 **kwargs: Unpack[RunOptions[TContext]],
455 ) -> RunResult:
456 context = kwargs.get("context")
457 max_turns = kwargs.get("max_turns", DEFAULT_MAX_TURNS)
458 hooks = cast(RunHooks[TContext], validate_run_hooks(kwargs.get("hooks")))
459 run_config = kwargs.get("run_config")
460 error_handlers = kwargs.get("error_handlers")
461 previous_response_id = kwargs.get("previous_response_id")
462 auto_previous_response_id = kwargs.get("auto_previous_response_id", False)
463 conversation_id = kwargs.get("conversation_id")
464 session = kwargs.get("session")
465
466 if run_config is None:
467 run_config = RunConfig()
468
469 is_resumed_state = isinstance(input, RunState)
470 run_state: RunState[TContext] | None = None
471 starting_input = input if not is_resumed_state else None
472 original_user_input: str | list[TResponseInputItem] | None = None
473 session_input_items_for_persistence: list[TResponseInputItem] | None = (
474 [] if (session is not None and is_resumed_state) else None
475 )
476 # Track the most recent input batch we persisted so conversation-lock retries can rewind
477 # exactly those items (and not the full history).
478 last_saved_input_snapshot_for_rewind: list[TResponseInputItem] | None = None
479
480 if is_resumed_state:
481 run_state = cast(RunState[TContext], input)
482 (
483 conversation_id,
484 previous_response_id,
485 auto_previous_response_id,
486 ) = apply_resumed_conversation_settings(
487 run_state=run_state,
488 conversation_id=conversation_id,
489 previous_response_id=previous_response_id,
490 auto_previous_response_id=auto_previous_response_id,
491 )
492 validate_session_conversation_settings(
493 session,
494 conversation_id=conversation_id,
495 previous_response_id=previous_response_id,
496 auto_previous_response_id=auto_previous_response_id,
497 )
498 starting_input = run_state._original_input
499 original_user_input = copy_input_items(run_state._original_input)
500 prepared_input = normalize_resumed_input(original_user_input)
501
502 context_wrapper = resolve_resumed_context(
503 run_state=run_state,
504 context=context,
505 )
506 context = context_wrapper.context
507

Calls 15

hydrate_from_stateMethod · 0.95
set_traceMethod · 0.95
prepare_agentMethod · 0.95
track_server_itemsMethod · 0.95
enqueue_memory_resultMethod · 0.95
cleanupMethod · 0.95
validate_run_hooksFunction · 0.85
RunConfigClass · 0.85