Skip to content

Commit 08a1b71

Browse files
Python: updated python design and dev setup (#171)
* updated python design and dev setup * updated dev setup * updated dev setup
1 parent eafb333 commit 08a1b71

2 files changed

Lines changed: 517 additions & 105 deletions

File tree

docs/design/python-package-setup.md

Lines changed: 54 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
### Sample getting started code
2828
```python
2929
from typing import Annotated
30-
from agent_framework import Agent, ai_tool
30+
from agent_framework import Agent, ai_function
3131
from agent_framework.openai import OpenAIChatClient
3232

33-
@ai_tool(description="Get the current weather in a given location")
33+
@ai_function(description="Get the current weather in a given location")
3434
async def get_weather(location: Annotated[str, "The location as a city name"]) -> str:
3535
"""Get the current weather in a given location."""
3636
# Implementation of the tool to get weather
@@ -50,12 +50,12 @@ print(response)
5050
Overall the following structure is proposed:
5151

5252
* agent-framework
53-
* tier 0; components, will be exposed directly from `agent_framework`:
53+
* core components, will be exposed directly from `agent_framework`:
5454
* (single) agents (includes threads)
5555
* tools (includes MCP and OpenAPI)
5656
* models/types (name tbd, will include the equivalent of MEAI for dotnet; content types and client abstractions)
5757
* logging
58-
* tier 1; components, will be exposed from `agent_framework.<component>`:
58+
* advanced components, will be exposed from `agent_framework.<component>`:
5959
* context_providers (tbd)
6060
* guardrails / filters
6161
* vector_data (vector stores and other MEVD pieces)
@@ -65,12 +65,12 @@ Overall the following structure is proposed:
6565
* utils (optional)
6666
* telemetry (could also be observability or monitoring)
6767
* workflows (includes multi-agent orchestration)
68-
* tier 2; extensions
69-
* Extensions are any additional functionality that is useful for a user, to reduce friction they will imported in a similar way as tier 1, however the code for them will be in a separate package, so that they can be installed separately, they must have everything in a folder with the same name as the package, and without a `__init__.py` file in the root, so that they can be used as a namespace package.
68+
* connectors; subpackages
69+
* Subpackages are any additional functionality that is useful for a user, to reduce friction they will imported in a similar way as advanced components, however the code for them will be in a separate package, so that they can be installed separately, they must expose all public items, in their main `__init__.py` file, so that they can be imported from the main package without additional import levels.
70+
In the main package a corresponding folder will be created, with a `__init__.py` file that lazy imports the public items from the subpackage, so that they can be exposed from the main package.
7071
* Some examples are:
71-
* openai
72-
* azure
73-
* will be exposed through i.e. `agent_framework.openai` and `agent_framework.azure`
72+
* azure (non LLM integrations)
73+
* will be exposed through i.e. `agent_framework.azure`
7474
* anything other then a connector that we want to expose as a separate package, for instance:
7575
* mem0 (memory management)
7676
* would be exposed through i.e. `agent_framework.mem0`
@@ -79,7 +79,6 @@ Overall the following structure is proposed:
7979
* tests
8080
* samples
8181
* extensions
82-
* openai
8382
* azure
8483
* ...
8584

@@ -90,53 +89,48 @@ Internal imports will be done using relative imports, so that the package can be
9089
The resulting file structure will be as follows:
9190

9291
```plaintext
93-
agent_framework/
94-
__init__.py
95-
__init__.pyi
96-
_agents.py
97-
_tools.py
98-
_models.py
99-
_logging.py
100-
context_providers.py
101-
guardrails.py
102-
exceptions.py
103-
evaluations.py
104-
utils.py
105-
telemetry.py
106-
templates.py
107-
text_search.py
108-
vector_data.py
109-
workflows.py
110-
py.typed
111-
extensions/
112-
mem0/
113-
agent_framework/
114-
mem0/
115-
__init__.py
116-
_mem0.py
117-
...
118-
redis/
119-
...
120-
openai/
92+
packages/
93+
main/
12194
agent_framework/
12295
openai/
12396
__init__.py
124-
_chat.py
125-
_embeddings.py
126-
...
97+
_chat_client.py
98+
_shared.py
99+
exceptions.py
100+
__init__.py
101+
__init__.pyi
102+
_agents.py
103+
_tools.py
104+
_models.py
105+
_logging.py
106+
context_providers.py
107+
guardrails.py
108+
exceptions.py
109+
evaluations.py
110+
utils.py
111+
telemetry.py
112+
templates.py
113+
text_search.py
114+
vector_data.py
115+
workflows.py
116+
py.typed
127117
tests/
128118
unit/
129-
test_openai_client.py
130-
test_openai_tools.py
131-
...
119+
test_types.py
132120
integration/
133-
test_openai_integration.py
134-
samples/ (optional)
135-
...
121+
test_chat_clients.py
136122
pyproject.toml
137123
README.md
138124
...
139-
google/
125+
mem0/
126+
agent_framework/
127+
mem0/
128+
__init__.py
129+
_mem0.py
130+
...
131+
redis/
132+
...
133+
google/
140134
agent_framework/
141135
google/
142136
__init__.py
@@ -156,13 +150,6 @@ extensions/
156150
README.md
157151
...
158152
...
159-
tests/
160-
__init__.py
161-
unit/
162-
conftest.py
163-
test_agents.py
164-
test_types.py
165-
...
166153
samples/
167154
...
168155
pyproject.toml
@@ -174,10 +161,10 @@ uv.lock
174161

175162
We might add a template subpackage as well, to make it easy to setup, this could be based on the first one that is added.
176163

177-
In the `DEV_SETUP.md` we will add instructions for how to deal with the path depth issues, especially on Windows, where the maximum path length can be a problem.
164+
In the [`DEV_SETUP.md`](../../python/DEV_SETUP.md) we will add instructions for how to deal with the path depth issues, especially on Windows, where the maximum path length can be a problem.
178165

179166
#### Evolving the package structure
180-
For each of the tier 1 components, we have two reason why we may split them into a folder, with an `__init__.py` and optionally a `_files.py`:
167+
For each of the advanced components, we have two reason why we may split them into a folder, with an `__init__.py` and optionally a `_files.py`:
181168
1. If the file becomes too large, we can split it into multiple `_files`, while still keeping the public interface in the `__init__.py` file, this is a non-breaking change
182169
2. If we want to partially or fully move that code into a separate package.
183170
In this case we do need to lazy load anything that was moved from the main package to the subpackage, so that existing code still works, and if the subpackage is not installed we can raise a meaningful error.
@@ -197,14 +184,7 @@ agent_framework/
197184

198185
## Coding standards
199186

200-
* We use google docstyles for docstrings.
201-
* We use the following setup for ruff:
202-
```toml
203-
[tool.ruff]
204-
line-length = 120
205-
target-version = "py310"
206-
include = ["*.py", "*.pyi", "**/pyproject.toml", "*.ipynb"]
207-
preview = true
187+
Coding standards will be maintained in the [`DEV_SETUP.md`](../../python/DEV_SETUP.md) file.
208188

209189
[tool.ruff.lint]
210190
fixable = ["ALL"]
@@ -281,16 +261,16 @@ The logging will be simplified, there will be one logger in the base package:
281261
* name: `agent_framework` - used for all logging in the abstractions and base components
282262
283263
Each of the other subpackages for connectors will have a similar single logger.
284-
* name: `agent_framework.connectors.openai`
285-
* name: `agent_framework.connectors.azure`
264+
* name: `agent_framework.openai`
265+
* name: `agent_framework.azure`
286266
287267
This means that when a logger is needed, it should be created like this:
288268
```python
289269
from agent_framework import get_logger
290270
291271
logger = get_logger()
292272
#or in a subpackage:
293-
logger = get_logger('agent_framework.connectors.openai')
273+
logger = get_logger('agent_framework.openai')
294274
```
295275
The implementation should be something like this:
296276
```python
@@ -300,18 +280,18 @@ import logging
300280
def get_logger(name: str = "agent_framework") -> logging.Logger:
301281
"""
302282
Get a logger with the specified name, defaulting to 'agent_framework'.
303-
283+
304284
Args:
305285
name (str): The name of the logger. Defaults to 'agent_framework'.
306-
286+
307287
Returns:
308288
logging.Logger: The configured logger instance.
309289
"""
310290
logger = logging.getLogger(name)
311291
# create the specifics for the logger, such as setting the level, handlers, etc.
312292
return logger
313293
```
314-
This will ensure that the logger is created with the correct name and configuration, and it will be consistent across the package.
294+
This will ensure that the logger is created with the correct name and configuration, and it will be consistent across the package.
315295

316296
Further there should be a easy way to configure the log levels, either through a environment variable or with a similar function as the get_logger.
317297

@@ -320,7 +300,7 @@ This will not be allowed:
320300
import logging
321301

322302
logger = logging.getLogger(__name__)
323-
```
303+
```
324304

325305
This is allowed but discouraged, if the get_logger function has been called at least once then this will return the same logger as the get_logger function, however that might not have happened and then the logging experience (in terms of formats and handlers, etc) is not consistent across the package:
326306
```python
@@ -332,40 +312,9 @@ logger = logging.getLogger("agent_framework")
332312
#### Telemetry
333313
Telemetry will be based on OpenTelemetry (OTel), and will be implemented in the `agent_framework.telemetry` package.
334314

335-
We should consider auto-instrumentation and provide an implementation of it to the OTel community.
336-
337-
### Function definitions
338-
To make the code easier to use, we will be very deliberate about the ordering and marking of function parameters.
339-
This means that we will use the following conventions:
340-
* Only parameters that are fully expected to be passed and only if there are a very limited number of them, let's say 3 or less, can they be supplied as positional parameters (still with a keyword, _almost_ never positional only).
341-
* All other parameters should be supplied as keyword parameters, this is especially important to configure correctly when using Pydantic or dataclasses.
342-
* If there are multiple required parameters, and they do not have a order that is common sense, then they will all use keyword parameters.
343-
* If we use `kwargs` we will document how and what we use them for, this might be a reference to a outside package's documentation or an explanation of what the `kwargs` are used for.
344-
* If we want to combine `kwargs` for multiple things, such as partly for a external client constructor, and partly for our own use, we will try to keep those separate, by adding a parameter, such as `client_kwargs` with type `dict[str, Any]`, and then use that to pass the kwargs to the client constructor (by using `Client(**client_kwargs)`), while using the `**kwargs` parameters for other uses, which are then also well documented.
345-
346-
### Attributes vs inheritance
315+
We will also add headers with user-agent strings where applicable, these will include `agent-framework-python` and the version.
347316

348-
When the parameters are the same except for one, we will use attributes, instead of inheritance, to minimize the conceptual overhead of understanding the code. Off course there are exceptions and these things will be decided on a case by case basis, but the general rule is that if the parameters are the same, we will use attributes.
349-
```python
350-
# ✅ preferred
351-
from agent_framework import ChatMessage
352-
user_msg = ChatMessage(
353-
role="user",
354-
content="Hello, world!"
355-
)
356-
asst_msg = ChatMessage(
357-
role="assistant",
358-
content="Hello, world!"
359-
)
360-
# ❌ not preferred
361-
from agent_framework import UserMessage, AssistantMessage
362-
user_msg = UserMessage(
363-
content="Hello, world!"
364-
)
365-
asst_msg = AssistantMessage(
366-
content="Hello, world!"
367-
)
368-
```
317+
We should consider auto-instrumentation and provide an implementation of it to the OTel community.
369318

370319
### Build and release
371320
The build step will be done in GHA, adding the package to the release and then we call into Azure DevOps to use the ESRP pipeline to publish to pypi. This is how SK already works, we will just have to adapt it to the new package structure.

0 commit comments

Comments
 (0)