Utilities¶
langsmith.utils
¶
Generic utility functions.
FUNCTION | DESCRIPTION |
---|---|
tracing_is_enabled |
Return True if tracing is enabled. |
test_tracking_is_disabled |
Return True if testing is enabled. |
xor_args |
Validate specified keyword args are mutually exclusive. |
raise_for_status_with_text |
Raise an error with the response text. |
get_enum_value |
Get the value of a string enum. |
log_once |
Log a message at the specified level, but only once. |
get_messages_from_inputs |
Extract messages from the given inputs dictionary. |
get_message_generation_from_outputs |
Retrieve the message generation from the given outputs. |
get_prompt_from_inputs |
Retrieve the prompt from the given inputs. |
get_llm_generation_from_outputs |
Get the LLM generation from the outputs. |
get_docker_compose_command |
Get the correct docker compose command for this system. |
convert_langchain_message |
Convert a LangChain message to an example. |
is_base_message_like |
Check if the given object is similar to BaseMessage. |
is_env_var_truish |
Check if the given environment variable is truish. |
get_env_var |
Retrieve an environment variable from a list of namespaces. |
get_tracer_project |
Get the project name for a LangSmith tracer. |
filter_logs |
Temporarily adds specified filters to a logger. |
get_cache_dir |
Get the testing cache directory. |
filter_request_headers |
Filter request headers based on ignore_hosts and allow_hosts. |
with_cache |
Use a cache for requests. |
with_optional_cache |
Use a cache for requests. |
deepish_copy |
Deep copy a value with a compromise for uncopyable objects. |
is_version_greater_or_equal |
Check if the current version is greater or equal to the target version. |
parse_prompt_identifier |
Parse a string in the format of owner/name:hash, name:hash, owner/name, or name. |
get_api_url |
Get the LangSmith API URL from the environment or the given value. |
get_api_key |
Get the API key from the environment or the given value. |
get_workspace_id |
Get workspace ID. |
get_host_url |
Get the host URL based on the web URL or API URL. |
is_truish |
Check if the value is truish. |
LangSmithAPIError
¶
Bases: LangSmithError
Internal server error while communicating with LangSmith.
LangSmithRequestTimeout
¶
Bases: LangSmithError
Client took too long to send request body.
LangSmithUserError
¶
Bases: LangSmithError
User error caused an exception when communicating with LangSmith.
LangSmithRateLimitError
¶
Bases: LangSmithError
You have exceeded the rate limit for the LangSmith API.
LangSmithAuthError
¶
Bases: LangSmithError
Couldn't authenticate with the LangSmith API.
LangSmithNotFoundError
¶
Bases: LangSmithError
Couldn't find the requested resource.
LangSmithConflictError
¶
Bases: LangSmithError
The resource already exists.
LangSmithConnectionError
¶
Bases: LangSmithError
Couldn't connect to the LangSmith API.
LangSmithExceptionGroup
¶
LangSmithWarning
¶
Bases: UserWarning
Base class for warnings.
LangSmithMissingAPIKeyWarning
¶
Bases: LangSmithWarning
Warning for missing API key.
FilterPoolFullWarning
¶
FilterLangSmithRetry
¶
LangSmithRetry
¶
Bases: Retry
Wrapper to filter logs with this name.
ContextThreadPoolExecutor
¶
Bases: ThreadPoolExecutor
ThreadPoolExecutor that copies the context to the child thread.
METHOD | DESCRIPTION |
---|---|
submit |
Submit a function to the executor. |
map |
Return an iterator equivalent to stdlib map. |
submit
¶
Submit a function to the executor.
PARAMETER | DESCRIPTION |
---|---|
func
|
The function to submit.
TYPE:
|
*args
|
The positional arguments to the function.
TYPE:
|
**kwargs
|
The keyword arguments to the function.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Future[T]
|
Future[T]: The future for the function. |
map
¶
map(
fn: Callable[..., T],
*iterables: Iterable[Any],
timeout: Optional[float] = None,
chunksize: int = 1,
) -> Iterator[T]
Return an iterator equivalent to stdlib map.
Each function will receive its own copy of the context from the parent thread.
PARAMETER | DESCRIPTION |
---|---|
fn
|
A callable that will take as many arguments as there are passed iterables.
TYPE:
|
timeout
|
The maximum number of seconds to wait. If None, then there is no limit on the wait time. |
chunksize
|
The size of the chunks the iterable will be broken into before being passed to a child process. This argument is only used by ProcessPoolExecutor; it is ignored by ThreadPoolExecutor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Iterator[T]
|
An iterator equivalent to: map(func, *iterables) but the calls may |
Iterator[T]
|
be evaluated out-of-order. |
RAISES | DESCRIPTION |
---|---|
TimeoutError
|
If the entire result iterator could not be generated before the given timeout. |
Exception
|
If fn(*args) raises for any values. |
tracing_is_enabled
¶
Return True if tracing is enabled.
xor_args
¶
Validate specified keyword args are mutually exclusive.
raise_for_status_with_text
¶
raise_for_status_with_text(response: Union[Response, Response]) -> None
Raise an error with the response text.
log_once
cached
¶
Log a message at the specified level, but only once.
get_messages_from_inputs
¶
Extract messages from the given inputs dictionary.
PARAMETER | DESCRIPTION |
---|---|
inputs
|
The inputs dictionary. |
RETURNS | DESCRIPTION |
---|---|
list[dict[str, Any]]
|
List[Dict[str, Any]]: A list of dictionaries representing the extracted messages. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If no message(s) are found in the inputs dictionary. |
get_message_generation_from_outputs
¶
Retrieve the message generation from the given outputs.
PARAMETER | DESCRIPTION |
---|---|
outputs
|
The outputs dictionary. |
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
Dict[str, Any]: The message generation. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If no generations are found or if multiple generations are present. |
get_prompt_from_inputs
¶
Retrieve the prompt from the given inputs.
PARAMETER | DESCRIPTION |
---|---|
inputs
|
The inputs dictionary. |
RETURNS | DESCRIPTION |
---|---|
str
|
The prompt.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the prompt is not found or if multiple prompts are present. |
get_llm_generation_from_outputs
¶
Get the LLM generation from the outputs.
get_docker_compose_command
cached
¶
Get the correct docker compose command for this system.
convert_langchain_message
¶
convert_langchain_message(message: BaseMessageLike) -> dict
Convert a LangChain message to an example.
is_base_message_like
¶
is_env_var_truish
¶
Check if the given environment variable is truish.
get_env_var
cached
¶
get_env_var(
name: str,
default: Optional[str] = None,
*,
namespaces: tuple = ("LANGSMITH", "LANGCHAIN"),
) -> Optional[str]
Retrieve an environment variable from a list of namespaces.
PARAMETER | DESCRIPTION |
---|---|
name
|
The name of the environment variable.
TYPE:
|
default
|
The default value to return if the environment variable is not found. Defaults to None. |
namespaces
|
A tuple of namespaces to search for the environment variable. Defaults to ("LANGSMITH", "LANGCHAINs").
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[str]
|
Optional[str]: The value of the environment variable if found, otherwise the default value. |
get_tracer_project
cached
¶
Get the project name for a LangSmith tracer.
filter_logs
¶
Temporarily adds specified filters to a logger.
Parameters: - logger: The logger to which the filters will be added. - filters: A sequence of logging.Filter objects to be temporarily added to the logger.
get_cache_dir
¶
filter_request_headers
¶
filter_request_headers(
request: Any,
*,
ignore_hosts: Optional[Sequence[str]] = None,
allow_hosts: Optional[Sequence[str]] = None,
) -> Any
Filter request headers based on ignore_hosts and allow_hosts.
with_cache
¶
with_cache(
path: Union[str, Path],
ignore_hosts: Optional[Sequence[str]] = None,
allow_hosts: Optional[Sequence[str]] = None,
) -> Generator[None, None, None]
Use a cache for requests.
with_optional_cache
¶
with_optional_cache(
path: Optional[Union[str, Path]],
ignore_hosts: Optional[Sequence[str]] = None,
allow_hosts: Optional[Sequence[str]] = None,
) -> Generator[None, None, None]
Use a cache for requests.
deepish_copy
¶
Deep copy a value with a compromise for uncopyable objects.
PARAMETER | DESCRIPTION |
---|---|
val
|
The value to be deep copied.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
T
|
The deep copied value. |
is_version_greater_or_equal
¶
Check if the current version is greater or equal to the target version.
parse_prompt_identifier
¶
Parse a string in the format of owner/name:hash, name:hash, owner/name, or name.
PARAMETER | DESCRIPTION |
---|---|
identifier
|
The prompt identifier to parse.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[str, str, str]
|
Tuple[str, str, str]: A tuple containing (owner, name, hash). |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the identifier doesn't match the expected formats. |
get_api_url
¶
Get the LangSmith API URL from the environment or the given value.
get_api_key
¶
Get the API key from the environment or the given value.
get_host_url
cached
¶
Get the host URL based on the web URL or API URL.