forked from llamastack/llama-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: OpenAPI Generator for client SDK #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aegeiger
wants to merge
10
commits into
release-0.4.x
Choose a base branch
from
feature/release-0.4.x-openapi
base: release-0.4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
456ab63
client-sdk: add SDK build infrastructure
aegeiger 74746df
client-sdk: add hierarchical APIs and streaming
aegeiger e03e58b
client-sdk: add async client support
aegeiger 02990e8
client-sdk: add lib exports for Agent & MCP OAuth
aegeiger 615b94d
client-sdk: add templates and server fixes
aegeiger 6340d30
Fix linting errors and remove duplicate templates/lib directory
aegeiger 82e6a1b
client-sdk: fix model dict serialization for falsy values
aegeiger af46bd6
client-sdk: add RateLimitError and httpx-style exception properties
aegeiger db88535
tests: remove invalid BadRequestException references
aegeiger 3f2f116
docs: add Stainless vs OpenAPI Generator SDK comparison
aegeiger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the terms described in the LICENSE file in | ||
| # the root directory of this source tree. | ||
|
|
||
| # Complete workflow to build hierarchical Python SDK | ||
| # | ||
| # This script: | ||
| # 1. Processes the OpenAPI spec to extract tag hierarchies | ||
| # 2. Generates the Python SDK using the processed spec | ||
| # 3. Patches the generated SDK to add hierarchical properties | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Colors for output | ||
| GREEN='\033[0;32m' | ||
| BLUE='\033[0;34m' | ||
| YELLOW='\033[0;33m' | ||
| RED='\033[0;31m' | ||
| NC='\033[0m' # No Color | ||
|
|
||
| # Script directory | ||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
|
||
| # Configuration | ||
| SOURCE_SPEC="${1:-$SCRIPT_DIR/openapi.generator.yml}" | ||
| OUTPUT_DIR="${2:-$SCRIPT_DIR/sdks/python}" | ||
|
|
||
| echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}" | ||
| echo -e "${BLUE}║ Llama Stack Hierarchical Python SDK Builder ║${NC}" | ||
| echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}" | ||
| echo "" | ||
|
|
||
| # Step 1: Process OpenAPI spec | ||
| echo -e "${YELLOW}Step 1/3: Processing OpenAPI spec to extract hierarchy...${NC}" | ||
| echo "" | ||
|
|
||
| python3 "$SCRIPT_DIR/process_openapi_hierarchy.py" \ | ||
| --source "$SOURCE_SPEC" \ | ||
| --output "$SCRIPT_DIR/openapi-processed.yml" \ | ||
| --hierarchy "$SCRIPT_DIR/api-hierarchy.yml" | ||
|
|
||
| if [ $? -ne 0 ]; then | ||
| echo -e "${RED}✗ Failed to process OpenAPI spec${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "" | ||
| echo -e "${GREEN}✓ OpenAPI spec processed${NC}" | ||
| echo "" | ||
|
|
||
| # Step 2: Generate Python SDK | ||
| echo -e "${YELLOW}Step 2/3: Generating Python SDK...${NC}" | ||
| echo "" | ||
|
|
||
| "$SCRIPT_DIR/generate-python-sdk.sh" \ | ||
| "$SCRIPT_DIR/openapi-processed.yml" \ | ||
| "$OUTPUT_DIR" | ||
|
|
||
| if [ $? -ne 0 ]; then | ||
| echo -e "${RED}✗ Failed to generate Python SDK${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "" | ||
| echo -e "${GREEN}✓ Python SDK generated${NC}" | ||
| echo "" | ||
|
|
||
| # Step 3: Summary | ||
| echo -e "${BLUE}╔════════════════════════════════════════════════════════════════╗${NC}" | ||
| echo -e "${BLUE}║ Build Complete! ║${NC}" | ||
| echo -e "${BLUE}╚════════════════════════════════════════════════════════════════╝${NC}" | ||
| echo "" | ||
| echo "Generated files:" | ||
| echo " 📄 openapi-processed.yml - Processed OpenAPI spec" | ||
| echo " 📄 api-hierarchy.yml - API hierarchy structure" | ||
| echo " 📁 $OUTPUT_DIR - Generated Python SDK" | ||
| echo "" | ||
| echo "To install the SDK:" | ||
| echo " cd $OUTPUT_DIR" | ||
| echo " pip install -e ." | ||
| echo "" | ||
| echo "The SDK now supports hierarchical API access:" | ||
| echo " client.chat.completions.create(...) # Nested structure" | ||
| echo "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| #!/bin/bash | ||
|
|
||
| # Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| # All rights reserved. | ||
| # | ||
| # This source code is licensed under the terms described in the LICENSE file in | ||
| # the root directory of this source tree. | ||
|
|
||
| # Script to generate the Python SDK using openapi-generator-cli with custom templates | ||
| # | ||
| # This script generates a Python client SDK from the OpenAPI specification | ||
| # using custom templates that create a convenient LlamaStackClient wrapper class. | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| # Colors for output | ||
| GREEN='\033[0;32m' | ||
| BLUE='\033[0;34m' | ||
| RED='\033[0;31m' | ||
| NC='\033[0m' # No Color | ||
|
|
||
| # Script directory | ||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
|
||
| # Paths | ||
| OPENAPI_SPEC="${1:-$SCRIPT_DIR/openapi.generator.yml}" | ||
| CONFIG_FILE="$SCRIPT_DIR/openapi-config.json" | ||
| TEMPLATE_DIR="$SCRIPT_DIR/templates/python" | ||
| OUTPUT_DIR="${2:-$SCRIPT_DIR/sdks/python}" | ||
|
|
||
| echo -e "${BLUE}Llama Stack Python SDK Generator${NC}" | ||
| echo "==================================" | ||
| echo "" | ||
| echo "Usage: $0 [OPENAPI_SPEC] [OUTPUT_DIR]" | ||
| echo " OPENAPI_SPEC: Path to OpenAPI spec (default: openapi.generator.yml)" | ||
| echo " OUTPUT_DIR: Output directory (default: sdks/python)" | ||
| echo "" | ||
|
|
||
| # Check if openapi-generator-cli is installed | ||
| if ! command -v openapi-generator-cli &> /dev/null; then | ||
| echo -e "${RED}Error: openapi-generator-cli is not installed${NC}" | ||
| echo "" | ||
| echo "Please install it using one of the following methods:" | ||
| echo "" | ||
| echo "1. Using npm (recommended):" | ||
| echo " npm install -g @openapitools/openapi-generator-cli" | ||
| echo "" | ||
| echo "2. Using Homebrew (macOS):" | ||
| echo " brew install openapi-generator" | ||
| echo "" | ||
| echo "3. Download the JAR file:" | ||
| echo " Visit https://openapi-generator.tech/docs/installation" | ||
| exit 1 | ||
| fi | ||
|
|
||
| # Verify files exist | ||
| if [ ! -f "$OPENAPI_SPEC" ]; then | ||
| echo -e "${RED}Error: OpenAPI spec not found at $OPENAPI_SPEC${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ ! -f "$CONFIG_FILE" ]; then | ||
| echo -e "${RED}Error: Config file not found at $CONFIG_FILE${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| if [ ! -d "$TEMPLATE_DIR" ]; then | ||
| echo -e "${RED}Error: Template directory not found at $TEMPLATE_DIR${NC}" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo -e "${GREEN}✓${NC} OpenAPI Spec: $OPENAPI_SPEC" | ||
| echo -e "${GREEN}✓${NC} Config File: $CONFIG_FILE" | ||
| echo -e "${GREEN}✓${NC} Template Dir: $TEMPLATE_DIR" | ||
| echo -e "${GREEN}✓${NC} Output Dir: $OUTPUT_DIR" | ||
| echo "" | ||
|
|
||
| # Create output directory if it doesn't exist | ||
| mkdir -p "$OUTPUT_DIR" | ||
|
|
||
| echo -e "${BLUE}Generating Python SDK...${NC}" | ||
| echo "" | ||
|
|
||
| # Run openapi-generator-cli | ||
| openapi-generator-cli generate \ | ||
| -i "$OPENAPI_SPEC" \ | ||
| -g python \ | ||
| -c "$CONFIG_FILE" \ | ||
| -t "$TEMPLATE_DIR" \ | ||
| -o "$OUTPUT_DIR" \ | ||
| --additional-properties=generateSourceCodeOnly=false | ||
|
|
||
| echo "" | ||
| echo -e "${GREEN}✓ Python SDK generated successfully!${NC}" | ||
| echo "" | ||
|
|
||
| # Copy the lib/ directory as-is (contains non-templated utility modules) | ||
| echo -e "${BLUE}Copying lib/ directory...${NC}" | ||
| if [ -d "$TEMPLATE_DIR/lib" ]; then | ||
| cp -r "$TEMPLATE_DIR/lib" "$OUTPUT_DIR/llama_stack_client/" | ||
| echo -e "${GREEN}✓${NC} lib/ directory copied successfully" | ||
| else | ||
| echo -e "${RED}Warning: lib/ directory not found at $TEMPLATE_DIR/lib${NC}" | ||
| fi | ||
| echo "" | ||
|
|
||
| # Check if api-hierarchy.yml exists and patch the APIs | ||
| HIERARCHY_FILE="$SCRIPT_DIR/api-hierarchy.yml" | ||
| PATCH_SCRIPT="$SCRIPT_DIR/patch_api_hierarchy.py" | ||
|
|
||
| if [ -f "$HIERARCHY_FILE" ] && [ -f "$PATCH_SCRIPT" ]; then | ||
| echo -e "${BLUE}Patching API hierarchy...${NC}" | ||
| echo "" | ||
| python3 "$PATCH_SCRIPT" --hierarchy "$HIERARCHY_FILE" --sdk-dir "$OUTPUT_DIR" | ||
| echo "" | ||
| fi | ||
|
|
||
| echo "OpenAPI Spec: $OPENAPI_SPEC" | ||
| echo "Output directory: $OUTPUT_DIR" | ||
| echo "" | ||
| echo "To install the SDK, run:" | ||
| echo " cd $OUTPUT_DIR" | ||
| echo " pip install -e ." | ||
| echo "" | ||
| echo "Example usage:" | ||
| echo " from llama_stack_client import Configuration, LlamaStackClient" | ||
| echo " config = Configuration(host=\"http://localhost:8000\")" | ||
| echo " client = LlamaStackClient(config)" | ||
| echo " # Use client.chat, client.agents, etc." |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a typo - "ignroe" -> "Ignore"
Please replace with this:
Ignore generated files