The `Utility Functions` component, primarily encapsulated within `instructor/utils.py`, serves as a crucial support system for the entire `instructor` codebase. It provides a collection of general-purpose helper functions that are not tied to a specific domain but are essential for various core operations.
Components
Utility Functions
This component comprises a collection of general-purpose helper functions that are used across the instructor codebase. These utilities support various core components by performing common tasks such as extracting JSON from different text formats, determining the LLM provider from a base URL, converting messages, and managing usage statistics. While not a primary flow component, it provides essential foundational support.
extract_json
This function is responsible for robustly extracting JSON objects from various text formats, including those that might be embedded within markdown code blocks or contain extraneous text. It's crucial for parsing LLM responses.
get_llm_provider
Determines the LLM provider (e.g., OpenAI, Anthropic, Cohere) based on the base URL of the LLM client. This is vital for dynamic configuration and behavior based on the underlying model.
is_async
A utility function to check if a given callable (function or method) is an asynchronous function.
is_provider_openai
Checks if the detected LLM provider is OpenAI.
is_provider_anthropic
Checks if the detected LLM provider is Anthropic.
is_provider_cohere
Checks if the detected LLM provider is Cohere.
is_provider_google
Checks if the detected LLM provider is Google (e.g., Gemini, Vertex AI).
is_provider_azure_openai
Checks if the detected LLM provider is Azure OpenAI.
is_provider_fireworks
Checks if the detected LLM provider is Fireworks.
is_provider_mistral
Checks if the detected LLM provider is Mistral.
process_response
A component responsible for processing LLM responses, likely calling `extract_json`.
patch
A component responsible for dynamically modifying client behavior, likely using `is_async`.