Skip to content

Python: Add hosted agent sample for the agent harness#7010

Open
vaibhav-patel wants to merge 2 commits into
microsoft:mainfrom
vaibhav-patel:fix/6477-hosted-harness-sample
Open

Python: Add hosted agent sample for the agent harness#7010
vaibhav-patel wants to merge 2 commits into
microsoft:mainfrom
vaibhav-patel:fix/6477-hosted-harness-sample

Conversation

@vaibhav-patel

Copy link
Copy Markdown
Contributor

What this adds

A new Python sample, python/samples/04-hosting/af-hosting/local_responses_harness/, that hosts a harness agent (built with create_harness_agent) behind the same helper-first Responses hosting seam used by the existing local_responses/ sample.

Files:

  • app.py — a module-level FastAPI ASGI app whose target is a create_harness_agent(...) agent. Uses AgentState + the agent-framework-hosting-responses helpers (responses_to_run, responses_session_id, create_response_id, responses_from_run, responses_from_streaming_run) exactly like local_responses/app.py, with a native /responses route and Hypercorn startup.
  • call_server.py — a plain openai SDK client that drives a three-turn conversation (rotating previous_response_id) to exercise session continuity.
  • pyproject.toml — mirrors local_responses/pyproject.toml ([tool.uv.sources] wires the pre-PyPI agent-framework-hosting* packages to the repo).
  • README.md — mirrors the sibling READMEs (what it shows, prerequisites, how to run, production-readiness notes).
  • storage/.gitignore — same session-storage placeholder as the siblings.
  • Registers the sample in af-hosting/README.md.

The takeaway the sample makes concrete: a harness agent is just an Agent, so it drops straight into the existing AgentState / Responses-helper seam. The harness supplies the function-invocation loop, per-service-call persistence, compaction, and todo management on top of a single @tool.

Choices I made (issue had no spec)

#6477 has no body and no comments, so I followed the conventions of the existing 02-agents/harness/ and 04-hosting/af-hosting/ samples rather than inventing a new shape:

  • Placement / naming: added as a third sibling under af-hosting/, named local_responses_harness/ to match the existing local_responses/ and local_responses_workflow/ pair. It reuses that directory's exact layout (app.py + call_server.py + pyproject.toml + storage/).
  • Headless-friendly harness config: disabled the interactive-only features (plan/execute mode and the Textual console) and web search, and registered the tool with approval_mode="never_require" so a one-shot HTTP request never blocks on human tool approval. Kept todo management and compaction enabled so the target is a genuine harness agent, not a relabelled plain Agent.
  • Copied the repo copyright header verbatim onto every .py and followed SAMPLE_GUIDELINES.md (AzureCliCredential, over-documented, expected-output comment).

Happy to move or rename this (e.g. a top-level 04-hosting/harness-hosted-agent/, or folding it into local_responses/ as a variant) if maintainers prefer a different structure or scope — glad to adjust.

Verification

No cloud credentials were used; this was not run against Azure.

  • python3 -m py_compile app.py call_server.py — clean.
  • uv run ruff check and uv run ruff format --check on both files — clean.
  • Dry import in the workspace env resolves every symbol used (create_harness_agent, AgentState, all Responses helpers, FoundryChatClient, tool, ResponseStream).
  • create_agent() assembles the harness agent and create_session(session_id=...) succeeds (the exact call AgentState.get_or_create_session makes) — no network required.

Fixes #6477.

Copilot AI review requested due to automatic review settings July 9, 2026 09:18
@giles17 giles17 added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Jul 9, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new Python hosting sample under python/samples/04-hosting/af-hosting/local_responses_harness/ that mirrors the existing Responses-route + AgentState seam, but hosts a harness agent created via create_harness_agent(...) instead of a plain Agent.

Changes:

  • Registered the new sample in af-hosting/README.md.
  • Added a self-contained sample directory with FastAPI /responses route (app.py), a local OpenAI SDK client (call_server.py), and run/docs scaffolding (README.md, pyproject.toml, storage/.gitignore).

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
python/samples/04-hosting/af-hosting/README.md Adds the new local_responses_harness/ entry to the hosting samples index.
python/samples/04-hosting/af-hosting/local_responses_harness/storage/.gitignore Keeps session/storage artifacts out of git for the new sample.
python/samples/04-hosting/af-hosting/local_responses_harness/README.md Documents what the sample demonstrates, how to run it, and production-readiness cautions.
python/samples/04-hosting/af-hosting/local_responses_harness/pyproject.toml Defines dependencies and local uv git sources for hosting packages.
python/samples/04-hosting/af-hosting/local_responses_harness/call_server.py Adds a 3-turn OpenAI SDK client to exercise previous_response_id continuity.
python/samples/04-hosting/af-hosting/local_responses_harness/app.py Implements a FastAPI /responses route hosting a harness agent behind Responses helper functions.

Comment thread python/samples/04-hosting/af-hosting/local_responses_harness/app.py
Comment thread python/samples/04-hosting/af-hosting/local_responses_harness/app.py
…mple

Addresses PR review: disable the harness file-memory and file-access
providers so the headless sample doesn't expose file tools or write
outside storage/, and correct the app.py docstring to match
call_server.py (which takes no prompt argument).
@vaibhav-patel vaibhav-patel force-pushed the fix/6477-hosted-harness-sample branch from 919f0a5 to b36eb7f Compare July 9, 2026 10:21
@moonbox3 moonbox3 added the hosting Usage: [Issues, PRs], Target: all hosting related solutions label Jul 14, 2026
# sample their read/write file tools would expose unintended tools, create
# on-disk side effects outside storage/, and could block on tool approval.
disable_file_memory=True,
disable_file_access=True,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we update this to the current opt-in API please? main removed disable_file_access in #7094, so once this branch is brought up to date, the first /responses request resolves create_agent() and fails with TypeError: got an unexpected keyword argument 'disable_file_access'. Omitting this flag is now sufficient because file access is disabled unless file_access_store is supplied.

# `agent.run(...)` updates the session. Store it under the newly minted
# response id after the run so `previous_response_id=response_id` continues
# from this exact point.
await state.set_session(response_id, session)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could we avoid aliasing the mutable AgentSession here and in the streaming path? SessionStore retains object references, so continuing from turn 1 to create turn 2 mutates the object still stored under turn 1; branching again from turn 1 then sees turn 2, and concurrent branches mutate the same state. A per-run lock would only serialize this corruption, so preserving independently resolvable response IDs needs snapshot or copy semantics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs hosting Usage: [Issues, PRs], Target: all hosting related solutions python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Add hosted agent sample for agent harness

4 participants