Skip to content

Export google_a2a as a python module instead of common and agents#169

Closed
djsamseng wants to merge 9 commits intoa2aproject:mainfrom
djsamseng:prefixPythonPackage
Closed

Export google_a2a as a python module instead of common and agents#169
djsamseng wants to merge 9 commits intoa2aproject:mainfrom
djsamseng:prefixPythonPackage

Conversation

@djsamseng
Copy link
Copy Markdown
Contributor

@djsamseng djsamseng commented Apr 15, 2025

Sets up the file structure correctly to allow for the modules to be imported with a proper namespace.

Previously developers would have to

import common

Now developers can

import google_a2a.common

This will allow us to further enhance this module to become production ready in the future. And in the meantime it allows those of us experimenting with A2A to avoid namespace clashes.

See the samples/python/README.md for an example setup.

Addresses the issues raised in this comment

Test Plan:

  • Note I don't have a GOOGLE_API_KEY so I couldn't fully run, but it exit cleanly thus verifying the only changes (the top level imports)
uv run src/google_a2a/agents/langgraph
uv run src/google_a2a/hosts/cli
  • Additionally tested importing google_a2a from another project via uv add /path/to/A2A/samples/python and was able to import google_a2a.common.server and create and run an A2AServer. This verifies the server does in fact run.

@djsamseng djsamseng mentioned this pull request Apr 15, 2025
@djsamseng
Copy link
Copy Markdown
Contributor Author

@rajeshvelicheti any chance I could grab you to take a look at this before too many more conflicts arise with samples getting added?

This will also allow us to start writing tests as well as a Python A2A tutorial

@djsamseng
Copy link
Copy Markdown
Contributor Author

djsamseng commented Apr 18, 2025

Rebased and made changes to make this PR work again after the PRs that have been merged to main in the meantime.

Test Plan:

  • Run all agents via uv run . in their respective directories.
  • Run the cli via uv run google-a2a-cli from /samples/python

- Replace imports
```bash
find src -name '*.py' -exec sed -i 's/^import common/import google_a2a.common/' {} +
find src -name '*.py' -exec sed -i 's/^import hosts/import google_a2a.hosts/' {} +
find src -name '*.py' -exec sed -i 's/^import agents/import google_a2a.agents/' {} +
find src -name '*.py' -exec sed -i 's/^from common/from google_a2a.common/' {} +
find src -name '*.py' -exec sed -i 's/^from agents/from google_a2a.agents/' {} +
find src -name '*.py' -exec sed -i 's/^from hosts/from google_a2a.hosts/' {} +
```

- Double check (note: some manual fixups may be needed due to spaces)
```bash
git grep -n -i "import common"
git grep -n -i "import hosts"
git grep -n -i "import agents"
git grep -n -i "from common"
git grep -n -i "from hosts"
git grep -n -i "from agents"
```
find . -name '*.md' -exec sed -i 's,/samples/python/common,/samples/python/src/google_a2a/common,' {} +
find . -name '*.txt' -exec sed -i 's,/samples/python/common,/samples/python/src/google_a2a/common,' {} +
find . -name '*.md' -exec sed -i 's,/samples/python/hosts,/samples/python/src/google_a2a/hosts,' {} +
find . -name '*.txt' -exec sed -i 's,/samples/python/hosts,/samples/python/src/google_a2a/hosts,' {} +
find . -name '*.md' -exec sed -i 's,/samples/python/agents,/samples/python/src/google_a2a/agents,' {} +
find . -name '*.txt' -exec sed -i 's,/samples/python/agents,/samples/python/src/google_a2a/agents,' {} +
find . -name '*.pyproject.toml' -exec sed -i 's/a2a-samples/google-a2a/' {} +
@djsamseng djsamseng force-pushed the prefixPythonPackage branch from 3db2eea to 5a6ceda Compare April 25, 2025 02:48
@djsamseng
Copy link
Copy Markdown
Contributor Author

Rebased and retested.

Here's the full description of changes

Steps

Move files

mkdir src
mkdir src/google_a2a
git mv -f common src/google_a2a
git mv -f hosts src/google_a2a
git mv -f agents src/google_a2a

Update pyproject.toml

[project]
name = "google-a2a"
description = "Agent2Agent modules and samples"

[tool.hatch.build.targets.wheel]
packages = ["src/google_a2a"]

[tool.uv.workspace]
members = [
    "src/google_a2a/agents/crewai",
    "src/google_a2a/agents/google_adk",
    "src/google_a2a/agents/langgraph",
    "src/google_a2a/hosts/cli",
    "src/google_a2a/hosts/multiagent",
    "src/google_a2a/agents/llama_index_file_chat",
    "src/google_a2a/agents/semantickernel",
]

Replace imports

find src -name '*.py' -exec sed -i 's/^import common/import google_a2a.common/' {} +
find src -name '*.py' -exec sed -i 's/^import hosts/import google_a2a.hosts/' {} +
find src -name '*.py' -exec sed -i 's/^import agents/import google_a2a.agents/' {} +
find src -name '*.py' -exec sed -i 's/^from common/from google_a2a.common/' {} +
find src -name '*.py' -exec sed -i 's/^from agents/from google_a2a.agents/' {} +
find src -name '*.py' -exec sed -i 's/^from hosts/from google_a2a.hosts/' {} +
  • Double check (note: some manual fixups may be needed due to spaces)
git grep -n -i "import common"
git grep -n -i "import hosts"
git grep -n -i "import agents"
git grep -n -i "from common"
git grep -n -i "from hosts"
git grep -n -i "from agents"

Replace references

find . -name '*.md' -exec sed -i 's,/samples/python/common,/samples/python/src/google_a2a/common,' {} +
find . -name '*.txt' -exec sed -i 's,/samples/python/common,/samples/python/src/google_a2a/common,' {} +
find . -name '*.md' -exec sed -i 's,/samples/python/hosts,/samples/python/src/google_a2a/hosts,' {} +
find . -name '*.txt' -exec sed -i 's,/samples/python/hosts,/samples/python/src/google_a2a/hosts,' {} +
find . -name '*.md' -exec sed -i 's,/samples/python/agents,/samples/python/src/google_a2a/agents,' {} +
find . -name '*.txt' -exec sed -i 's,/samples/python/agents,/samples/python/src/google_a2a/agents,' {} +

Replace a2a-samples

find . -name '*.pyproject.toml' -exec sed -i 's/a2a-samples/google-a2a/' {} +
git grep -n -i a2a-samples

Create quickstart example

## Quickstart
1. Install google-a2a
    ```bash
    pip install git+https://github.com/google/A2A.git#subdirectory=samples/python
    ```
2. Implement your task manager
    ```python
    import typing
    import google_a2a
    import google_a2a.common
    import google_a2a.common.server
    import google_a2a.common.server.task_manager
    import google_a2a.common.types
    class MyTaskManager(google_a2a.common.server.task_manager.InMemoryTaskManager):
        def __init__(
            self,
        ):
            super().__init__()
        async def on_send_task(
            self,
            request: google_a2a.common.types.SendTaskRequest
        ) -> google_a2a.common.types.SendTaskResponse:
            """
            This method queries or creates a task for the agent.
            The caller will receive exactly one response.
            """
            pass

        async def on_send_task_subscribe(
            self,
            request: google_a2a.common.types.SendTaskStreamingRequest
        ) -> typing.AsyncInterable[google_a2a.common.types.SendTaskStreamingResponse] | google_a2a.common.types.JSONRPCResponse:
            """
            This method subscribes the caller to future updates regarding a task.
            The caller will receive a response and additionally receive subscription
            updates over a session established between the client and the server
            """
            pass

    ```
3. Run a server
    ```python
    import google_a2a
    import google_a2a.common
    import google_a2a.common.server
    import google_a2a.common.types
    def main():
        # See examples
        server = google_a2a.common.server.A2AServer(
            # Fill in parameters
            task_manager=MyTaskManager(),
        )
        server.start()
    ```

Expose cli as a script

[project.scripts]
google-a2a-cli = "google_a2a.hosts.cli.__main__:main"
  • hosts/cli/main.py
def main():
    asyncio.run(cli())


if __name__ == "__main__":
    main()

Test Plan

  • Run uv run . in each folder of /samples/python/src/google_a2a/agents
  • Run uv run . in /samples/python/src/google_a2a/hosts/cli
  • Run uv run google-a2a-cli in /samples/python
  • Follow the python tutorial and ensure imports work correctly

This was referenced Apr 25, 2025
@umariyoob
Copy link
Copy Markdown

umariyoob commented May 2, 2025

if this isn't merged in - the tutorial doesn't line up yet right? https://google.github.io/A2A/tutorials/python/7-interact-with-server/

I'm trying to run the cli from the my-project folder instead of having to pull the entire repo down.

@djsamseng
Copy link
Copy Markdown
Contributor Author

if this isn't merged in - the tutorial doesn't line up yet right? https://google.github.io/A2A/tutorials/python/7-interact-with-server/

I'm trying to run the cli from the my-project folder instead of having to pull the entire repo down.

Interesting - it was originally mentioned in the tutorial but now has overwritten https://github.com/google/A2A/pull/202/files#diff-1e612e5d30745859d2821e04adb1bd652320e755b78a814c2d24db71301cd0b4R37

The git history for the gh-pages branch also got force rewritten 😥

version = "0.2.0"
description = "Agent2Agent modules and samples"
readme = "README.md"
requires-python = ">=3.12"
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.

@djsamseng would it be possible for this to use >=3.11 ?
(see #406 )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Happy to - once we get a green light I’ll rebase as well.

@daavoo
Copy link
Copy Markdown
Contributor

daavoo commented May 6, 2025

anything blocking a review here?
It would be quite useful for people building stuff downstream

@holtskinner
Copy link
Copy Markdown
Member

See #423 for updates on official A2A SDK launch

anything blocking a review here? It would be quite useful for people building stuff downstream

@holtskinner
Copy link
Copy Markdown
Member

@djsamseng Thank you for this contribution, however much of it won't be needed going forward once the official SDK is released. There is an early version of it in a2a-python-sdk/ but this will soon be moved to its own repository and published on pypi.

@holtskinner holtskinner requested a review from a team as a code owner May 16, 2025 21:31
@holtskinner
Copy link
Copy Markdown
Member

holtskinner commented May 28, 2025

This PR is no longer needed due to the Python SDK being published.

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.

4 participants