Skip to content

Commit e0221e5

Browse files
authored
Python: Fix OpenAPI concept sample (#12418)
### Motivation and Context An OpenAPI concept sample that we have is broken. This PR fixes it to run properly based on the current OpenAPI plugin handling. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description Fixes the broken OpenAPI sample. <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
1 parent 576a49b commit e0221e5

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

python/samples/concepts/plugins/openapi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
For more generic setup instructions, including how to install the `uv` tool, see the [main README](../../../../DEV_SETUP.md).
44

5-
1. In a terminal, navigate to `semantic_kernel/python/samples/kernel-syntax-examples/openapi_example`.
5+
1. In a terminal, navigate to `semantic_kernel/python/samples/concepts/plugins/openapi`.
66

77
2. Run `uv sync` followed by `source .venv/bin/activate` to enter the virtual environment (depending on the os, the activate script may be in a different location).
88

python/samples/concepts/plugins/openapi/openapi.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ paths:
3232
description: Your name
3333
- name: Header
3434
in: header
35-
required: false
35+
required: true
3636
schema:
3737
type: string
3838
description: The header

python/samples/concepts/plugins/openapi/openapi_client.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

33
import asyncio
4+
import os
45

5-
import semantic_kernel as sk
6+
from semantic_kernel import Kernel
67
from semantic_kernel.functions.kernel_arguments import KernelArguments
78

89

910
async def main():
10-
"""Client"""
11-
kernel = sk.Kernel()
12-
13-
openapi_plugin = kernel.add_plugin_from_openapi(plugin_name="openApiPlugin", openapi_document_path="./openapi.yaml")
14-
15-
arguments = {
16-
"request_body": '{"input": "hello world"}',
17-
"path_params": '{"name": "mark"}',
18-
"query_params": '{"q": "0.7"}',
19-
"headers": '{"Content-Type": "application/json", "Header": "example"}',
20-
}
21-
22-
kernel_arguments = KernelArguments(**arguments)
23-
24-
result = await kernel.invoke(openapi_plugin["helloWorld"], arguments=kernel_arguments)
11+
"""OpenAPI Sample Client"""
12+
kernel = Kernel()
13+
14+
spec_path = os.path.join(
15+
os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))),
16+
"plugins",
17+
"openapi",
18+
"openapi.yaml",
19+
)
20+
21+
openapi_plugin = kernel.add_plugin_from_openapi(plugin_name="openApiPlugin", openapi_document_path=spec_path)
22+
23+
arguments = KernelArguments(
24+
input="hello world",
25+
name="John",
26+
q=0.7,
27+
Header="example-header",
28+
)
29+
30+
result = await kernel.invoke(openapi_plugin["helloWorld"], arguments=arguments)
2531

2632
print(result)
2733

python/samples/concepts/plugins/openapi/openapi_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from aiohttp import web
44

5-
"""Server"""
5+
"""OpenAPI Sample Server"""
66
routes = web.RouteTableDef()
77

88

0 commit comments

Comments
 (0)