Custom code regions in Python
To enable custom code regions for Python SDKs, update the project’s
.speakeasy/gen.yaml
file like so:
.speakeasy/gen.yaml
configVersion: 2.0.0generation:# ...python:# ...+ enableCustomCodeRegions: true
Full example
The Speakeasy examples repository has a full example (opens in a new tab) of a Python SDK that uses custom code regions.
Regions
Below are the available code regions in Python SDKs.
SDK classes
Python SDK classes can have two code regions:
# region imports
- allows the addition of imports to an SDK file needed for custom methods and properties. This region must be located at the top of the file alongside generated imports.# region sdk-class-body
- allows the addition of custom methods and properties to an SDK class. This region must be located in the body of a Python SDK class alongside generated methods and properties.
src/todos_sdk/todos.py
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""from .basesdk import BaseSDKfrom todos_sdk import models, utilsfrom todos_sdk._hooks import HookContextfrom todos_sdk.types import OptionalNullable, UNSETfrom typing import Mapping, Optional# region importsimport markdown# endregion importsclass Todos(BaseSDK):# region sdk-class-bodydef render_todo(self, id: str) -> str:todo = self.get_one(id=id)return markdown.markdown(f"# {todo.title}\n\n{todo.description}")# endregion sdk-class-bodydef get_one(self,*,id: int,retries: OptionalNullable[utils.RetryConfig] = UNSET,server_url: Optional[str] = None,timeout_ms: Optional[int] = None,http_headers: Optional[Mapping[str, str]] = None,) -> models.Todo:...async def get_one_async(self,*,id: int,retries: OptionalNullable[utils.RetryConfig] = UNSET,server_url: Optional[str] = None,timeout_ms: Optional[int] = None,http_headers: Optional[Mapping[str, str]] = None,) -> models.Todo:...