Pages

Topics

Tools

↑↓navigate↵openescclose

Python guides

Every published Python guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest Python guides

Python code streaming tokens from OpenAI API as a chat completion response
Python

How to Stream OpenAI Chat Completions in Python

Learn how to stream OpenAI chat completions in Python, handle token chunks, manage errors, and use async streaming for real-time responses.

OpenAI APIStreamingPythonAsync
A diagram showing a Python application receiving a tool call request from the OpenAI API and executing a function
Python

Python OpenAI Tool Calling and Function Calling

python openai tool calling and function calling: Implement OpenAI tool calling in Python: define tool schemas, detect tool_calls in responses, execute functions, and r...

OpenAITool CallingFunction CallingPython SDK
Illustration of a Python code block transforming into a structured JSON object with a schema diagram, symbolizing OpenAI structured outputs.
Python

Python OpenAI Structured Outputs with JSON Schema

Learn how to use OpenAI structured outputs in Python to enforce a JSON schema on model responses, parse the returned JSON, and validate it with Pydantic.

OpenAI APIJSON SchemaStructured OutputsPython
Diagram of a Python script sending a request to the OpenAI Responses API and receiving generated text.
Python

Python OpenAI Responses API for Text Generation

python openai responses api text generation: Learn how to use the OpenAI Responses API in Python for text generation, including request parameters, response handling,...

OpenAIPythonText GenerationResponses API
Python code interacting with the OpenAI Responses API, showing a request and response flow.
Python

Python OpenAI Responses API Usage: Practical Examples

Learn how to use the OpenAI Responses API from Python: making requests, handling responses, streaming, error handling, and production considerations.

OpenAIResponses APIPythonLLM
A visual representation of sentence embeddings being normalized to unit length for cosine similarity computation.
Python

How to Normalize Embeddings with Python Sentence Transformers

python sentence transformers normalize embeddings: Learn how to normalize embeddings from Python sentence-transformers for cosine similarity, including the normalize_e...

sentence-transformersembeddingsnormalizationcosine similarity
Illustration of multiple sentence embeddings being generated in parallel on a GPU chip
Python

Python Sentence Transformers Batch Encoding and GPU

Learn how to batch encode texts with sentence-transformers on a GPU, control batch size, avoid out-of-memory errors, and choose the right output format for embeddings.

sentence-transformersGPUbatch encodingembeddings
Illustration of sentence embeddings mapped as points in a vector space, with a query point and its nearest neighbors highlighted to represent semantic search.
Python

Python Sentence Transformers: Cosine Similarity and Semantic Search

Learn how to use Python sentence transformers and cosine similarity to build semantic search. Covers model loading, embeddings, similarity scoring, vector indexes, and practical trade-offs.

sentence-transformerscosine similaritysemantic searchembeddings
A diagram showing two sentences mapped to vectors in a high-dimensional space, with a cosine similarity angle between them.
Python

Python Sentence Transformers: Embeddings and Semantic Similarity

Learn how to generate sentence embeddings with Python's sentence-transformers library and compute semantic similarity with cosine similarity. The article also covers model selection, batching, and production considerations.

sentence-transformersembeddingssemantic-similaritynatural-language-processing
A diagram showing the Trainer class orchestrating a fine-tuning workflow with TrainingArguments controlling batch size, epochs, and evaluation.
Python

Fine-Tuning with Python Transformers Trainer and TrainingArguments

python transformers trainer trainingarguments and fine tuning: Learn how to fine-tune transformer models with the Python transformers Trainer and TrainingArguments, co...

Hugging FaceFine-tuningTrainer APITrainingArguments