Skip to content

Add Python examples and playbooks#426

Draft
welteki wants to merge 2 commits intoopenfaas:masterfrom
welteki:add-python-examples
Draft

Add Python examples and playbooks#426
welteki wants to merge 2 commits intoopenfaas:masterfrom
welteki:add-python-examples

Conversation

@welteki
Copy link
Copy Markdown
Member

@welteki welteki commented Apr 3, 2026

Description

Add detailed, step-by-step examples to the Python language docs.

Examples:

  • boto3 S3 example — accessing AWS S3 from a Python function with secret-based credential management and client reuse (tested e2e)
  • Kafka producer example — publishing messages to a Kafka topic using confluent-kafka with SASL/SSL authentication, including a note on common SASL mechanisms (tested e2e)
  • OpenAI Chat API example — calling the OpenAI Chat Completions API using the official openai SDK with secret-based API key management and client reuse (tested e2e)
  • SSE streaming example — streaming Server-Sent Events from a Python function using the python3-flask template with a Flask Response generator (tested e2e)
  • Streaming OpenAI responses — combining SSE with the OpenAI API to stream chat completions token by token (tested e2e)
  • ECR with IRSA example — managing AWS ECR repositories using boto3 with IAM Roles for Service Accounts (IRSA) on EKS for ambient credential discovery, replacing the need for static secrets (tested e2e)
  • OpenFaaS REST API example — creating a namespace and deploying a function using the OpenFaaS REST API with basic auth (tested e2e)
  • Playwright web testing example — running headless browser tests from a Python function using Playwright (tested e2e)

Motivation and Context

Make it easier for users to get started with common patterns by providing ready-to-use examples in the official documentation.

  • I have raised an issue to propose this change (required)

How Has This Been Tested?

All examples have been tested end-to-end: functions were built, deployed to a live OpenFaaS cluster, and invoked to verify correct behaviour. SSE streaming was verified to produce chunked responses with the Accept: text/event-stream header.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I've read the CONTRIBUTION guide
  • I have signed-off my commits with git commit -s

@reviewfn

This comment has been minimized.

@reviewfn

This comment has been minimized.

@reviewfn

This comment has been minimized.

@reviewfn

This comment has been minimized.

welteki added 2 commits April 8, 2026 14:45
Add step-by-step examples for common patterns: boto3 S3, Kafka
producer, OpenAI Chat API, SSE streaming, streaming OpenAI responses,
ECR with IRSA, OpenFaaS REST API, and Playwright web testing.

Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
@welteki welteki force-pushed the add-python-examples branch from a973faa to 64a9399 Compare April 8, 2026 12:45
@reviewfn
Copy link
Copy Markdown

reviewfn bot commented Apr 8, 2026

AI Pull Request Overview

Summary

  • Adds 8 comprehensive Python examples to OpenFaaS docs: S3 with boto3, Kafka producer, OpenAI API (full and streaming), Server-Sent Events, ECR with IRSA, OpenFaaS REST API usage, and Playwright web testing.
  • Each example includes code, requirements, stack.yaml, and step-by-step deployment instructions.
  • Renames docs/languages/python.md to docs/languages/python/index.md and updates with links to new examples.
  • Updates mkdocs.yml navigation to include the new example pages.
  • Examples demonstrate common patterns: client reuse for efficiency, secret management, different templates (python3-http, python3-http-debian, python3-flask, dockerfile), and authentication methods.

Approval rating (1-10)

7 - Valuable additions to documentation with thorough examples and testing, but contains critical JSON parsing bugs that would cause runtime failures in several examples.

Summary per file

Summary per file
File path Summary
docs/languages/python/examples/ecr-irsa.md Adds ECR repository management example using IRSA for ambient AWS credentials
docs/languages/python/examples/kafka.md Adds Kafka message publishing example with SASL/SSL authentication
docs/languages/python/examples/openai-streaming.md Adds OpenAI streaming completions example using Server-Sent Events
docs/languages/python/examples/openai.md Adds OpenAI Chat API example with client reuse
docs/languages/python/examples/openfaas-api.md Adds OpenFaaS REST API usage example for programmatic function deployment
docs/languages/python/examples/playwright.md Adds web testing example with Playwright using custom Dockerfile
docs/languages/python/examples/s3-boto3.md Adds S3 object storage example with boto3
docs/languages/python/examples/sse.md Adds Server-Sent Events streaming example
docs/languages/python/index.md Renames from python.md, updates navigation and adds example links
mkdocs.yml Updates navigation to include Python example pages

Overall Assessment

The PR significantly enhances the Python documentation by providing practical, tested examples covering common integration patterns. The examples follow OpenFaaS best practices like secret management and client reuse. However, critical bugs in JSON parsing would cause runtime failures, requiring fixes before merge.

Detailed Review

Detailed Review

Critical Issues

JSON Parsing Bugs in Multiple Examples

In docs/languages/python/examples/ecr-irsa.md, docs/languages/python/examples/openfaas-api.md, and docs/languages/python/examples/playwright.md:

  • Code uses json.loads(event.body) where event.body is bytes in the python3-http template
  • This will raise TypeError: the JSON object must be str, bytes or bytearray, not 'bytes' at runtime
  • Fix: Change to json.loads(event.body.decode('utf-8')) to properly decode bytes to string before parsing

Inconsistent Request Body Handling in Documentation

In docs/languages/python/index.md:

  • Example shows str(event.body) which for bytes input produces "b'input'" instead of the actual content
  • This is misleading and incorrect for practical use
  • Should demonstrate proper decoding: event.body.decode('utf-8')

Code Quality Observations

Positive Patterns

  • All examples properly use OpenFaaS secrets for sensitive data (API keys, passwords)
  • Client/session reuse implemented to avoid initialization overhead on every request
  • Template selection appropriate: python3-http-debian for native dependencies, python3-flask for streaming responses, dockerfile for complex setups
  • Error handling present where relevant (ECR repository checks, API status codes)
  • Security-conscious: no secrets logged or exposed in environment variables

Suggestions for Improvement

  • In docs/languages/python/examples/kafka.md: Topic name 'faas-request' is hardcoded - consider making configurable via environment variable for production flexibility
  • In docs/languages/python/examples/openai-streaming.md: The streaming loop assumes chunk.choices[0].delta.content exists and is truthy, but OpenAI API can return chunks with no content (e.g., function calls) - add None check for robustness
  • In docs/languages/python/examples/ecr-irsa.md: The repository creation doesn't handle potential IAM permission issues gracefully - could add more specific error handling for AWS API errors

Documentation Quality

Strengths

  • Step-by-step instructions with CLI commands and expected outputs
  • Clear use-case descriptions and prerequisites
  • Consistent structure across all examples
  • Links to related documentation and external resources
  • Testing claims verified with end-to-end testing

Minor Improvements

  • In docs/languages/python/examples/playwright.md: The custom Dockerfile explanation could note why the standard template isn't sufficient (Playwright's Chromium dependencies)
  • Consider adding performance notes for resource-intensive examples (Playwright, OpenAI streaming) regarding memory limits and concurrency

Security Review

  • No security issues found
  • Proper secret usage throughout
  • IRSA example correctly demonstrates credential-less AWS access
  • Basic auth in REST API example uses secrets appropriately

Consistency with OpenFaaS Ecosystem

  • Examples align with existing OpenFaaS patterns and conventions
  • Template usage follows documented guidelines
  • Stack YAML formatting consistent with other docs
  • Navigation updates properly integrate with MkDocs structure

Testing and Validation

  • PR states all examples tested end-to-end - this is commendable and should be maintained
  • No suggestions for additional tests needed, as examples are integration-focused rather than unit-testable business logic

AI agent details.

Agent processing time: 46.791s
Environment preparation time: 4.407s
Total time from webhook: 55.19s

@alexellis
Copy link
Copy Markdown
Member

image

I remember you saying that all the examples had been tested. But it may still be worth trying the updated version to see if it continues to work.

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.

2 participants