Internal Architecture¶
The package is intentionally layered so that public LangChain behavior can stay explicit and testable.
Read the codebase from top to bottom in this order:
public entry points
connection.py,resources.py,vectorstores.py,embeddings.py,space_embedders.py, anderrors.pyvalidators local input validation that rejects malformed values before the SDK is touched
behavior layer memory-operation helpers that normalize batch writes and streamed retrieval
transport layer the only layer that talks directly to the official GoodMem SDK
internal normalized types dataclasses and protocols that keep the layers decoupled
That split is what lets the repository test most edge cases without requiring a live GoodMem deployment for every change.
The embeddings bootstrap path follows the same layering rule:
embeddings.pyowns the publicGoodMemEmbeddings.ensure(...)andensure_from_env(...)entry pointsresources.pyowns the publicGoodMemResourcesfacade for embedders, spaces, memories, and vector-store bootstrap_internal.providersowns bootstrap matching, request normalization, server-side embedder resolution, and local embeddings readiness checks_internal.transportowns the SDK calls needed by the resource facade and LangChain-facing classes_internal.typesowns the package-local bootstrap request and the narrow transport protocols used by upper layers
Private GoodMem memory-operation helpers built on the transport boundary.
This module implements the behavior layer between the public vector-store API and the transport adapter.
Responsibilities:
turn normalized write requests into stable batch outcomes
preserve request ordering even when the backend returns explicit
request_indexvaluesdecode streamed retrieval events into flat
GoodMemSearchHitvaluesnormalize duplicate-ID and partial-success behavior into package-owned errors
- create_space(transport, request)[source]¶
Create a GoodMem space through the transport boundary.
- Parameters:
transport (
SupportsMemoryOperationsTransport) – Transport implementation exposing create-space behavior.request (
GoodMemSpaceCreateRequest) – Normalized create-space payload.
- Returns:
str– The non-emptyspace_idstring extracted from the transport response for the newly created GoodMem space.- Raises:
GoodMemAPIError – If GoodMem does not return a usable
space_id.
- add_memories(transport, *, space_id, writes)[source]¶
Create one batch of GoodMem memories.
- Parameters:
transport (
SupportsMemoryOperationsTransport) – Transport implementation exposing batch-create behavior.space_id (
str) – Target GoodMem space ID.writes (
list[GoodMemWriteRequest]) – Normalized write payloads in caller order.
- Returns:
list[str] – Memory IDs in the original request order.- Raises:
GoodMemDuplicateIDError – If GoodMem reports duplicate IDs.
GoodMemBatchPartialFailureError – If only part of the batch succeeds.
GoodMemAPIError – If the response shape is invalid or the backend rejects the batch.
- delete_memories(transport, *, memory_ids)[source]¶
Delete a batch of GoodMem memories.
- Parameters:
transport (
SupportsMemoryOperationsTransport) – Transport implementation exposing batch-delete behavior.memory_ids (
list[str]) – GoodMem memory IDs to delete.
- Raises:
GoodMemAPIError – If the response is missing, malformed, reports failed selectors, or does not match the requested delete count.
- Return type:
None
- search_memories(transport, *, space_id, query, k, filter_expression=None)[source]¶
Run chunk-level semantic retrieval and flatten the streamed response.
- Parameters:
transport (
SupportsMemoryOperationsTransport) – Transport implementation exposing retrieval behavior.space_id (
str) – Target GoodMem space ID.query (
str) – Semantic retrieval query.k (
int) – Maximum number of hits to collect.filter_expression (
str|None) – Optional raw GoodMem filter expression string.
- Returns:
list[GoodMemSearchHit] – FlatGoodMemSearchHitvalues in retrieval order.
Private helpers for the GoodMem embeddings path.
This module owns the provider-facing logic behind GoodMemEmbeddings.
Responsibilities:
normalize the GoodMem embedder response into
GoodMemEmbedderConfigvalidate that the selected embedder can back an
OPENAI-compatible LangChain embeddings adapternormalize and validate bootstrap requests used by embedder bootstrap helpers
find compatible existing embedders before create-once bootstrap falls back to provisioning a new one
resolve credentials and optional dimensions overrides
verify that bootstrap-resolved embedders are immediately usable by the current embeddings path
resolve server-side embedders without local provider initialization for resource bootstrap
build the upstream provider
Embeddingsimplementation lazily
- load_embedder_config(transport, *, embedder_id)[source]¶
Load and validate one GoodMem embedder configuration.
- Parameters:
transport (
SupportsEmbedderTransport) – Transport implementation exposing embedder lookup.embedder_id (
str) – GoodMem embedder ID to resolve.
- Returns:
GoodMemEmbedderConfig– A normalized, validated embedder configuration.- Raises:
GoodMemConfigurationError – If the embedder shape is incompatible with
GoodMemEmbeddings.GoodMemAPIError – If GoodMem lookup fails.
- create_provider_embeddings(embedder, *, upstream_api_key_override=None)[source]¶
Build the upstream LangChain embeddings provider for one embedder.
- Parameters:
embedder (
GoodMemEmbedderConfig) – Normalized GoodMem embedder configuration.upstream_api_key_override (
str|None) – Optional explicit upstream API key that takes precedence over inline or environment-based resolution.
- Returns:
Embeddings– AnEmbeddingsimplementation backed bylangchain_openai.OpenAIEmbeddings.- Raises:
GoodMemConfigurationError – If optional dependencies or upstream credentials are missing.
GoodMemAPIError – If provider initialization fails.
- ensure_embedder(transport, *, request, upstream_api_key_override=None)[source]¶
Find or create one compatible embedder and verify it is usable.
- Parameters:
transport (
SupportsEmbedderTransport) – Transport implementation exposing embedder bootstrap operations.request (
GoodMemEmbedderBootstrapRequest) – Normalized bootstrap request describing the target embedder.upstream_api_key_override (
str|None) – Optional explicit upstream API key used during readiness validation when the resolved embedder does not expose a readable inline secret.
- Returns:
GoodMemEmbedderConfig– The validated embedder configuration that satisfiesGoodMemEmbeddingsrequirements. Existing matches are revalidated for compatibility and readiness before they are reused.- Raises:
GoodMemConfigurationError – If the bootstrap request is invalid, if more than one compatible embedder exists, or if the resolved embedder is unusable.
GoodMemAPIError – If GoodMem list, create, or get operations fail.
- ensure_server_side_embedder(transport, *, request)[source]¶
Find or create one compatible GoodMem embedder for server-side retrieval.
This helper resolves the GoodMem resource needed to create a searchable space without importing or initializing any local LangChain embeddings provider. It is intentionally narrower than
ensure_embedder(...): existing matches are reused based on provider shape, and newly created embedders receive the explicit API key fromrequestor theGOODMEM_EMBEDDINGS_API_KEYfallback.- Parameters:
transport (
SupportsEmbedderTransport) – Transport implementation exposing embedder bootstrap operations.request (
GoodMemEmbedderBootstrapRequest) – Bootstrap request describing the target server-side embedder.
- Returns:
GoodMemEmbedderConfig– The validated embedder configuration that can be attached to a GoodMem space for server-side add/search workflows.- Raises:
GoodMemConfigurationError – If the bootstrap request is invalid, if more than one compatible embedder exists, or if the resolved embedder is not attachable to a text-search space.
GoodMemAPIError – If GoodMem list or create operations fail.
- default_bootstrap_display_name()[source]¶
Return the fixed default display name for bootstrap-created embedders.
- Return type:
str
- validate_bootstrap_request(request, *, operation_name='GoodMemEmbeddings.ensure')[source]¶
Validate and normalize one bootstrap request.
- Parameters:
request (
GoodMemEmbedderBootstrapRequest) – Candidate bootstrap request supplied by the public entry point.operation_name (
str) – Public operation name used in validation errors.
- Returns:
GoodMemEmbedderBootstrapRequest– A normalized bootstrap request.- Raises:
GoodMemConfigurationError – If any field is unsupported, blank, or invalid for the selected bootstrap flow.
- find_matching_embedders(transport, *, request)[source]¶
Return compatible embedders visible to the current client.
- Parameters:
transport (
SupportsEmbedderTransport) – Transport implementation exposing embedder listing.request (
GoodMemEmbedderBootstrapRequest) – Normalized bootstrap request describing the desired embedder.
- Returns:
list[GoodMemEmbedderConfig] – Compatible embedder configurations in transport-provided order.
- find_server_side_matching_embedders(transport, *, request)[source]¶
Return compatible embedders for server-side GoodMem retrieval.
- Return type:
list[GoodMemEmbedderConfig]
- embedder_matches_bootstrap_request(embedder, *, request)[source]¶
Check whether one embedder satisfies the requested bootstrap target.
- Return type:
bool
- format_provider_failure_message(prefix, exc)[source]¶
Format one provider failure message with bounded detail text.
- Parameters:
prefix (
str) – Stable message prefix that identifies the failing operation.exc (
Exception) – Original exception raised by the provider or setup path.
- Returns:
str– A human-readable error string that includes bounded exception detail text when any detail is available.
- resolve_upstream_api_key(embedder, *, upstream_api_key_override=None)[source]¶
Resolve the upstream provider API key for one embedder configuration.
- Parameters:
embedder (
GoodMemEmbedderConfig) – Normalized GoodMem embedder configuration.upstream_api_key_override (
str|None) – Optional explicit upstream API key that takes precedence over inline or environment-based resolution.
- Returns:
str– The API key that should be forwarded to the upstream embeddings provider.- Raises:
GoodMemConfigurationError – If neither inline credentials nor the environment fallback can satisfy the selected embedder.
- resolve_upstream_dimensions(embedder)[source]¶
Resolve the optional dimensions override for the upstream provider.
- Parameters:
embedder (
GoodMemEmbedderConfig) – Normalized GoodMem embedder configuration.- Returns:
int|None– The configured dimensions override, orNonewhen the environment variable is unset.- Raises:
GoodMemConfigurationError – If the environment value is invalid or does not match the GoodMem embedder dimensionality.
- validate_compatible_embedder_config(embedder)[source]¶
Validate that a GoodMem embedder can back
GoodMemEmbeddings.- Parameters:
embedder (
GoodMemEmbedderConfig) – Normalized GoodMem embedder configuration.- Returns:
None–Nonewhen the embedder configuration satisfies all provider compatibility requirements.- Raises:
GoodMemConfigurationError – If provider type, modality, endpoint, model, dimensions, or API path assumptions are not satisfied.
- validate_server_side_embedder_config(embedder)[source]¶
Validate that an embedder can be attached for server-side text search.
Unlike
validate_compatible_embedder_config(...), this does not require local credential forwarding or a LangChain OpenAI-compatible API path.- Return type:
None
- build_provider_base_url(embedder)[source]¶
Build the upstream provider base URL from endpoint and API path.
- Parameters:
embedder (
GoodMemEmbedderConfig) – Normalized GoodMem embedder configuration.- Returns:
str– The upstream provider base URL expected bylangchain_openai.OpenAIEmbeddingsafter removing any terminal/embeddingspath segment.
Shared private validators used by public entry points.
This module keeps local input validation in package-owned code so public entry points can reject malformed values before transport code or the GoodMem SDK is invoked.
- require_non_empty_trimmed_string(value, *, error_message, exception_type)[source]¶
Require one strict string input to be present and non-blank.
- Parameters:
value (
str|None) – Candidate string value to validate.error_message (
str) – Message used when validation fails.exception_type (
type[Exception]) – Exception class raised for invalid input.
- Returns:
str– The trimmed string value.- Raises:
Exception – An instance of
exception_typewhenvalueis missing, not a string, or blank after trimming.
- require_verify_value(verify)[source]¶
Validate the
verifyshape accepted byGoodMemConnection.- Parameters:
verify (
bool|str) – TLS verification value supplied by the caller.- Returns:
bool|str– Either the original boolean value or a validated non-empty string path.- Raises:
GoodMemConfigurationError – If
verifyis neither a boolean nor a non-empty string.
- require_space_id(space_id)[source]¶
Validate one existing-space identifier.
- Parameters:
space_id (
str) – Existing GoodMem space identifier supplied by the caller.- Returns:
str– The trimmed GoodMem space identifier.- Raises:
GoodMemConfigurationError – If
space_idis missing, not a string, or blank.
- require_embedder_id(embedder_id)[source]¶
Validate one explicit GoodMem embedder identifier.
- Parameters:
embedder_id (
str) – Explicit GoodMem embedder identifier supplied by the caller.- Returns:
str– The trimmed GoodMem embedder identifier.- Raises:
GoodMemConfigurationError – If
embedder_idis missing, not a string, or blank.
- validate_text_inputs(texts, *, label, exception_type, field_name=None)[source]¶
Validate one list of text inputs and preserve original ordering.
- Parameters:
texts (
list[Any]) – Candidate text values to validate.label (
str) – Logical collection name used in validation errors.exception_type (
type[Exception]) – Exception class raised when validation fails.field_name (
str|None) – Optional field label appended to error messages for nested sources such asDocument.page_content.
- Returns:
list[str] – The validated string values in original order, preserving their original text content.- Raises:
Exception – An instance of
exception_typewhen any value is not a string or is blank after trimming.
- validate_lengths(label, expected_length, *, metadatas=None, ids=None)[source]¶
Validate metadata and ID list lengths against one text/document count.
- Parameters:
label (
str) – Logical collection name used in validation errors.expected_length (
int) – Required number of entries.metadatas (
list[Any] |None) – Optional metadata list aligned to the source collection.ids (
list[str|None] |None) – Optional ID list aligned to the source collection.
- Returns:
None–Nonewhen both optional lists matchexpected_length.- Raises:
ValueError – If either optional list length does not match
expected_length.
- normalize_metadatas(metadatas)[source]¶
Normalize metadata mappings into plain dictionaries.
- Parameters:
metadatas (
list[Mapping[str,Any] |None] |None) – Optional metadata values aligned to a write input collection.- Returns:
list[dict[str,Any]] |None–NonewhenmetadatasisNone; otherwise one plain-dictionary metadata value per input item, withNoneentries converted to empty dictionaries.- Raises:
ValueError – If any non-
Nonemetadata value is not a mapping.
- normalize_space_embedders(embedders)[source]¶
Validate and normalize create-time space-embedder declarations.
- Parameters:
embedders (
list[GoodMemSpaceEmbedder]) – Candidate create-time GoodMem space-embedder declarations.- Returns:
list[GoodMemSpaceEmbedder] – A validated list ofGoodMemSpaceEmbeddervalues.- Raises:
GoodMemConfigurationError – If
embeddersis empty or contains values other thanGoodMemSpaceEmbedderinstances.
- normalize_optional_ids(ids, *, source, exception_type)[source]¶
Normalize one aligned list of optional strict-create memory IDs.
- Parameters:
ids (
list[str|None] |None) – Optional aligned memory ID list.source (
str) – Logical source name used in validation errors.exception_type (
type[Exception]) – Exception class raised when validation fails.
- Returns:
list[str|None] |None–NonewhenidsisNone; otherwise a normalized list whose entries are eitherNoneor non-empty strings.- Raises:
Exception – An instance of
exception_typewhen any value is neitherNonenor a non-empty string.
- validate_duplicate_ids(ids)[source]¶
Reject repeated non-
Nonememory IDs in one local write call.- Parameters:
ids (
list[str|None] |None) – Optional aligned memory ID list to inspect.- Returns:
None–NonewhenidsisNoneor when all non-NoneIDs are unique.- Raises:
GoodMemDuplicateIDError – If the same non-
Nonememory ID appears more than once.
- validate_similarity_search_inputs(*, query, k, filter_expression)[source]¶
Validate the public similarity-search input shape.
- Parameters:
query (
str) – Candidate semantic retrieval query text.k (
int) – Requested result count.filter_expression (
str|None) – Optional raw GoodMem filter expression string.
- Returns:
str– The validated query string.- Raises:
ValueError – If
queryis blank,kis not a positive integer, orfilter_expressionis neitherNonenor a string.
- raise_for_unexpected_kwargs(operation, kwargs)[source]¶
Reject service-specific keyword arguments that the package does not support.
- Parameters:
operation (
str) – Operation name used in the error message.kwargs (
dict[str,Any]) – Keyword arguments provided by the caller.
- Returns:
None–Nonewhenkwargsis empty.- Raises:
ValueError – If any unsupported keyword arguments are present.
Official GoodMem SDK transport boundary with normalized exceptions.
This module is the only package layer that talks directly to the GoodMem SDK.
Responsibilities:
construct the SDK client from
GoodMemConnectionmap package-owned request shapes onto SDK request objects
expose the narrow embedder list/get/create calls needed by the bootstrap embeddings path
normalize SDK-specific exceptions into package-owned errors
preserve consumer exceptions when retrieval streams are consumed lazily
- class GoodMemTransport(connection)[source]¶
Official GoodMem SDK backed transport implementation.
- Parameters:
connection (
GoodMemConnection) – Validated GoodMem transport configuration used to construct the underlying SDK client.- Raises:
GoodMemConfigurationError – If the optional
goodmemdependency is not installed.
- create_space(request)[source]¶
Create a GoodMem space from one normalized request payload.
- Parameters:
request (
GoodMemSpaceCreateRequest) – Package-owned create-space payload.- Returns:
Any– The raw GoodMem SDK response object for the created space.- Raises:
GoodMemDuplicateIDError – If GoodMem reports that the space already exists.
GoodMemAPIError – If the SDK rejects the request or raises any other backend failure.
- list_spaces(*, label=None, name_filter=None, max_items=None)[source]¶
List GoodMem spaces visible to the current client.
- Return type:
Any
- batch_create_memories(*, space_id, writes)[source]¶
Create one batch of GoodMem memories from normalized write payloads.
- Parameters:
space_id (
str) – Target GoodMem space ID.writes (
list[GoodMemWriteRequest]) – Package-owned memory-write payloads in request order.
- Returns:
Any– The raw GoodMem SDK batch-create response.- Raises:
GoodMemDuplicateIDError – If GoodMem reports duplicate memory IDs.
GoodMemAPIError – If the SDK rejects the request or raises any other backend failure.
- retrieve_memories(*, space_id, query, k, filter_expression=None)[source]¶
Yield one GoodMem retrieval stream while normalizing setup failures.
- Parameters:
space_id (
str) – Target GoodMem space ID.query (
str) – Semantic retrieval query text.k (
int) – Requested result count forwarded to GoodMem.filter_expression (
str|None) – Optional raw GoodMem filter expression string.
- Yields:
The SDK-managed stream of retrieval events.
- Raises:
GoodMemAPIError – If stream setup or stream teardown fails for a backend reason. Consumer exceptions raised while iterating the yielded events are preserved as-is.
- Return type:
Iterator[Any]
- create_memory(request)[source]¶
Create one GoodMem memory from a normalized request.
- Return type:
Any
- get_memory(*, memory_id, include_content=False)[source]¶
Load one GoodMem memory response by ID.
- Return type:
Any
- list_memories(*, space_id, filter_expression=None, max_items=None)[source]¶
List GoodMem memories in one space.
- Return type:
Any
- delete_memories(*, memory_ids)[source]¶
Delete one batch of GoodMem memories by ID.
- Return type:
Any
- get_embedder(*, embedder_id)[source]¶
Load one GoodMem embedder response by ID.
- Parameters:
embedder_id (
str) – GoodMem embedder identifier to resolve.- Returns:
Any– The raw GoodMem SDK embedder response.- Raises:
GoodMemAPIError – If GoodMem rejects the lookup or any other backend failure occurs.
- list_embedders(*, label=None, owner_id=None, provider_type=None)[source]¶
List GoodMem embedders visible to the current client.
- Returns:
Any– The raw GoodMem SDK embedder listing response.- Raises:
GoodMemAPIError – If GoodMem rejects the listing or any other backend failure occurs.
- create_embedder(request)[source]¶
Create one GoodMem embedder from a normalized bootstrap request.
- Parameters:
request (
GoodMemEmbedderBootstrapRequest) – Package-owned bootstrap request describing the embedder to create.- Returns:
Any– The raw GoodMem SDK response object for the created embedder.- Raises:
GoodMemDuplicateIDError – If GoodMem reports that the embedder already exists.
GoodMemAPIError – If the SDK rejects the request or raises any other backend failure.