CastleGuard Python SDK
The CastleGuard Python SDK provides a convenient interface to interact with CastleGuard's API. This SDK allows you to authenticate, log messages, interact with the chatbot, translate text, perform named entity recognition (NER), transcribe audio files, manage document collections, work with vision-based models, and more.
Features
- Authentication: Authenticate with an API key (no session required) or with a username and password.
- Logging: Send log messages to CastleGuard's logging endpoint.
- Chatbot Integration: Interact with CastleGuard's chatbot to generate responses based on input prompts, with or without document collections.
- Translation: Translate text between languages, supporting English and French.
- Named Entity Recognition (NER): Perform NER on text to extract important entities.
- Vision: Perform image-based tasks using a vision model by uploading an image with a descriptive prompt.
- Transcription: Upload and transcribe audio files, and download SRT subtitles.
- Collections: Manage document collections by creating, uploading, and retrieving the status of files.
- Heartbeat: Fetch the health status of various system components.
- Text Extraction: Extract paragraphs from raw text or extract text from a document file.
Installation
Install the package via pip:
pip install castleguard-sdk
Usage
Initialization
The CastleGuard class supports two authentication modes. Pick one — you do not
need both.
API key (recommended for services and integrations):
from castleguard_sdk import CastleGuard
cg = CastleGuard(base_url='https://your-castleguard-url', api_key='your-api-key')
Username and password:
from castleguard_sdk import CastleGuard
cg = CastleGuard(base_url='https://your-castleguard-url', username='your-username', password='your-password')
base_url may be given with or without the API version. If no /api/vN suffix
is present, /api/v1 is appended (override with default_version='v2').
Authentication
| Mode | Constructor arguments | Token exchange on init | Session |
|---|---|---|---|
| API key | api_key=... |
None | Stateless |
| Username/password | username=..., password=... |
POST /auth/token |
Bearer token held for the life of the instance |
API key mode. The key is used directly as the credential and sent as
Authorization: Bearer <api_key> on every request. No call to /auth/token is
made, so there is no session to establish, expire, or refresh. This makes the
SDK usable from short-lived processes, serverless functions, and request
handlers where a login round-trip per instance would be wasteful.
Username/password mode. The SDK calls /auth/token during __init__ and
holds the returned token on the instance. The token is not refreshed
automatically; construct a new CastleGuard instance if it expires.
Notes that apply to both modes:
-
api_keytakes precedence. If you passapi_keyalongsideusernameandpassword, the username and password are ignored and no token exchange happens. -
The constructor raises
ValueErrorwhen credentials are missing. If you pass neither anapi_keynor a complete username/password pair, no client is handed back, so a half-configured object can never reach your call sites. -
A client that holds no token sends no
Authorizationheader, rather than a literalBearer None. In username/password mode the token exchange happens in__init__, so checkcg.tokenbefore relying on the client: if it isNone, the credentials were rejected or the tenant was unreachable. -
Keep credentials out of source control. Read them from the environment:
import os from castleguard_sdk import CastleGuard cg = CastleGuard( base_url=os.environ['CASTLEGUARD_BASE_URL'], api_key=os.environ['CASTLEGUARD_API_KEY'], )
Verifying your authentication setup
Four scripts in python/examples check that authentication is
wired up correctly — an offline check and a live smoke test for each credential
type.
The offline checks need no credentials and make no network calls. They stub the HTTP layer and assert the behaviour described above:
python examples/api-key-offline-check.py
python examples/password-offline-check.py
api-key-offline-check.py covers API key mode: that it performs no token
exchange, that the key is sent as a bearer credential, and that it takes
precedence over a username and password.
password-offline-check.py covers username/password mode: that __init__
POSTs {"account": ..., "password": ...} to /auth/token, that the returned
token is stored and sent as Authorization: Bearer <token>, that rejected
credentials, a token-less response, and a network error each leave token as
None rather than raising, and that missing credentials raise ValueError.
Lines it prints as GAP are known weaknesses that are reported without failing
the run; there are none open at the moment.
The live smoke tests hit a real tenant and read credentials from the environment:
export CASTLEGUARD_BASE_URL='https://your-castleguard-url'
export CASTLEGUARD_API_KEY='your-api-key'
python examples/api-key-test.py
export CASTLEGUARD_USERNAME='your-account'
export CASTLEGUARD_PASSWORD='your-password'
python examples/password-test.py
api-key-test.py calls heartbeat() with an API key and no username or
password, and confirms that an invalid key is rejected. password-test.py
calls heartbeat() with an account and password and no API key, and confirms
that a wrong password and an unknown account are both rejected.
Run the live tests against a test tenant, not production.
Logging
Log a message with a specific log level (default is 1):
cg.log("This is a log message", logLevel=2)
Chatbot Interaction
Send a prompt to the chatbot and get a response:
response, chat_id = cg.chat("What is the weather today?")
print(response)
You can also chat within the context of a document collection:
response, chat_id = cg.chat_with_collection("Summarize the document", collection_id="your-collection-id")
print(response)
Translation
1. Instant Text Translation
Translate a single text with translate_text:
translated_text = cg.translate_text("Hello, how are you?", source_lang="en", target_lang="fr")
print(translated_text)
Parameters
- text: The text string to be translated.
- source_lang: Source language code.
- target_lang: Target language code.
Returns
- The translated text. Returns
Noneif an error occurs.
2. Multiple Text Translations
Translate multiple texts simultaneously with translate_texts:
texts = ["Hello", "Goodbye"]
source_langs = ["en", "eng"]
target_langs = ["fr", "deu"]
translated_texts = cg.translate_texts(texts, source_langs, target_langs)
print(translated_texts)
Parameters
- texts: List of text strings to translate.
- source_langs: List of source language codes, one for each text.
- target_langs: List of target language codes, one for each text.
Returns
-
A list of dictionaries, where each dictionary contains:
- originalText: The original text provided.
- translatedText: The translated text.
- sourceLanguageCode: The source language code.
- targetLanguageCode: The target language code.
Returns
Noneif an error occurs.
Note: The lists for texts, source_langs, and target_langs must be of equal length.
3. Document Translation
Upload an audio file and translate it:
document_id = cg.translate_document("path/to/file", source_lang="en", target_lang="fr")
print(f"Document ID: {document_id}")
Parameters
- file_path: Path to the file to be translated. This path can be relative to the file where this function is called (e.g.,
.or../) or an absolute path. - source_lang: Source language code.
- target_lang: Target language code.
- keep_original: Whether to retain the original document (default is True).
Returns
- A
document_idfor the created document.Noneif an error occurs.
4. Translate Multiple Documents
Translate multiple documents:
file_paths = ["/path/tofile1.txt", "/path/tofile2.docx"]
document_ids = cg.translate_documents(file_paths, source_lang="en", target_lang="fr")
print(f"Document IDs: {document_ids}")
Parameters
-
file_path_list: List of file paths to be translated. Like the single-file method, each path can be relative to the file where this function is called (e.g.,
.or../) or an absolute path. -
source_lang: Source language code.
-
target_lang: Target language code.
-
keep_original: Whether to retain the original document (default is True).
Returns
The function returns a list of dictionaries, each corresponding to a document. Each dictionary contains the following keys:
document_id: A unique identifier for the document.status: The outcome of the translation, either"fail"or"success".status_code: A numerical status code (e.g.,2for finished).status_name: A descriptive status name (e.g.,"finished").
In case of an error, the function will return None.
5. Downloading Documents
Original Document
saved_original_path = cg.download_translation_original(document_id, save_path="./downloads", file_name="original_file.txt")
Translated Document
saved_translated_path = cg.download_translation_translated(document_id, save_path="./downloads", file_name="translated_file.txt")
Parameters
- document_id: The ID of the document.
- save_path: The path where the document will be saved. This can be either a relative path (e.g.,
.or../) or an absolute path. If not provided, the file will be saved in the current working directory by default. - file_name: The name of the file to save the document as. If not provided, the default file name will be auto-generated based on the original file name, source and target languages, and document type (e.g., 'original' or 'translated').
Returns
- The path where the document will be saved. If any error occurs, it will return
None.
6. Get Translation Document Status
Single Document
Check the status of a single document:
status_code = cg.get_translation_document_status(document_id)
print(status_code)
Parameters
- document_id: The ID of the document.
Returns
- The status code (e.g., "success", "failed") if successful,
Noneif an error occurs.
Multiple Documents
Check the status of multiple documents at once:
document_ids = [document_id1, document_id2]
statuses = cg.get_translation_documents_status(document_ids)
print(statuses)
Parameters
- document_ids: A list of document IDs to check the status for.
Returns
- A list of dictionaries, each containing:
"document_id": Unique identifier for the document."status": Translation outcome, either"fail"or"success"."status_code": Numerical status code (e.g.,2for finished,0for queued)."status_name": Status description (e.g.,"finished","queued").
- In case of an error, the function will return
None.
Named Entity Recognition (NER)
Perform NER on text to extract entities:
2. Process Text
entities = cg.named_entity_recognition("John Doe works at Google in Mountain View.")
print(entities)
Parameters
- text: The input text for performing NER. The function will analyze the provided text to identify entities such as names, locations, dates, and other key terms.
Returns
- A dictionary containing the original text and a list of extracted entities. The entities are structured as dictionaries with:
entityType: The type of entity (e.g., "PERSON", "GPE", "DATE", "ORG").value: The actual entity extracted from the text.startPosition: The start index of the entity in the text.endPosition: The end index of the entity in the text.
Process Multiple Texts
To process multiple texts at once:
texts = ["John Doe works at Google.", "Jane Smith is a doctor in New York."]
entities_list = cg.named_entity_recognition_multiple_text(texts)
print(entities_list)
Parameters
- text_list: A list of input texts for performing NER on multiple documents.
Returns
- A list of dictionaries, where each dictionary contains:
- The
textfield with the original text. - The
entitiesfield, which is a list of extracted entities for that text.
- The
Vision
Upload an image file and provide a descriptive prompt to perform vision-based tasks:
vision_result = cg.vision(prompt="Detect objects in this image", file_path="/path/to/image.jpg")
print(vision_result)
Transcription
1. Document Translation
Upload an audio file and transcribe it:
document_id = cg.transcribe("/path/to/audiofile.mp3")
Parameters
- file_path: Path to the audio file to transcribe. This path can be relative to the file where this function is called (e.g.,
.or../) or an absolute path. - diarization_config: (Optional) Diarization configuration. Default is 1.
Returns
- A
document_idfor the created document.Noneif an error occurs.
2. Transcribe Multiple Documents
Transcribe multiple documents:
file_paths = ["/path/to/audiofile1.mp3", "/path/to/audiofile2.mp3"]
transcription_status = cg.transcribe_multiple(file_paths)
print(transcription_status)
Parameters
-
file_path_list: List of paths to the audio files to transcribe. Like the single-file method, each path can be relative to the file where this function is called (e.g.,
.or../) or an absolute path. -
diarization_config: (Optional) Diarization configuration. Default is 1.
Returns
The function returns a list of dictionaries, each corresponding to a document. Each dictionary contains the following keys:
document_id: A unique identifier for the document.status: The outcome of the translation, either"fail"or"success".status_code: A numerical status code (e.g.,2for finished).status_name: A descriptive status name (e.g.,"finished").
In case of an error, the function will return None.
3. Downloading Documents
Original Document
saved_file_path = cg.download_transcription_original_file(document_id, save_path="./transcriptions")
SRT Document
saved_srt_path = cg.download_srt_file(document_id, save_path="./subtitles")
Transcription Document
transcription_text_path = cg.download_transcription_file(document_id, save_path="./transcriptions")
Parameters
- document_id: The ID of the document.
- save_path: The path where the document will be saved. This can be either a relative path (e.g.,
.or../) or an absolute path. If not provided, the file will be saved in the current working directory by default. - file_name: Name for the saved file. If not provided, it will be auto-generated based on the original file name and document type (e.g., 'original' or 'SRT').
Returns
- The path where the document will be saved. If any error occurs, it will return
None.
4. Download SRT Text Directly
To download the SRT text directly without saving it to a file, use download_srt:
srt_text = cg.download_srt(document_id)
print(srt_text)
Parameters
- document_id: The ID of the transcription document.
Returns
- The SRT text if successful,
Noneif an error occurs.
5. Get Transcription Document Status
Single Document
Check the status of a single document:
status_code = cg.get_transcription_document_status(document_id)
print(status_code)
Parameters
- document_id: The ID of the document.
Returns
- The status code (e.g., "success", "failed") if successful,
Noneif an error occurs.
Multiple Documents
Check the status of multiple documents at once:
document_ids = [document_id1, document_id2]
statuses = cg.get_transcription_documents_status(document_ids)
print(statuses)
Parameters
- document_ids: A list of document IDs to check the status for.
Returns
- A list of dictionaries, each containing:
"document_id": Unique identifier for the document."status": Translation outcome, either"fail"or"success"."status_code": Numerical status code (e.g.,2for finished,0for queued)."status_name": Status description (e.g.,"finished","queued").
- In case of an error, the function will return
None.
Anonymizer
1. Anonymize Document
Upload an file and anonymize it:
document_id = cg.anonymize_document(
'/path/to/file1.txt',
[EntityType.ADDRESS.value],
)
Parameters
- file_path: Path to the file to anonymize. This path can be relative to the file where this function is called (e.g.,
.or../) or an absolute path. - options: List of
EntityTypevalues to anonymize. If not provided, no entity-based anonymization will be performed. - regex: Regular expression to match and anonymize additional custom data. If not provided, no regex-based anonymization will be performed.
Returns
- A
document_idfor the created document.Noneif an error occurs.
2. Anonymize Multiple Documents
Anonymize multiple documents:
documents_status = cg.anonymize_documents(
['/path/to/file1.txt', '/path/to/file2.txt',],
[EntityType.PERSON.value, EntityType.WORK_OF_ART.value],
"\\b2019\\b"
)
Parameters
- file_path_list: List of paths to the files to anonymize. Like the single-file method, each path can be relative to the file where this function is called (e.g.,
.or../) or an absolute path. - options: List of
EntityTypevalues to anonymize. If not provided, no entity-based anonymization will be performed. - regex: Regular expression to match and anonymize additional custom data. If not provided, no regex-based anonymization will be performed.
Returns
The function returns a list of dictionaries, each corresponding to a document. Each dictionary contains the following keys:
document_id: A unique identifier for the document.status: The outcome of the translation, either"fail"or"success".status_code: A numerical status code (e.g.,2for finished).status_name: A descriptive status name (e.g.,"finished").
In case of an error, the function will return None.
Entity Types for Anonymization
The following EntityType values are available for anonymization. You can specify a list of these entities when calling the anonymization methods:
EntityType.ADDRESS
EntityType.CARDINAL
EntityType.COMPANY
EntityType.CRYPTO
EntityType.DATE
EntityType.DNS
EntityType.EMAIL
EntityType.EVENT
EntityType.FAC
EntityType.GPE
EntityType.IP
EntityType.LANGUAGE
EntityType.LAW
EntityType.LOC
EntityType.MONEY
EntityType.NORP
EntityType.ORDINAL
EntityType.ORG
EntityType.PERCENT
EntityType.PERSON
EntityType.PHONE
EntityType.PRODUCT
EntityType.QUANTITY
EntityType.TIME
EntityType.WORK_OF_ART
3. Downloading Documents
Original Document
saved_file_path = cg.download_anonymized_original_documnet(document_id, save_path="./downloads",file_name="original")
Anonymized Document
saved_srt_path = cg.download_anonymized_documnet(document_id, save_path="./subtitles",file_name="original")
Parameters
- document_id: The ID of the document.
- save_path: The path where the document will be saved. This can be either a relative path (e.g.,
.or../) or an absolute path. If not provided, the file will be saved in the current working directory by default. - file_name: Name for the saved file. If not provided, it will be auto-generated based on the original file name and document type (e.g., 'original' or 'anonymized').
Returns
- The path where the document will be saved. If any error occurs, it will return
None.
5. Get Anonymized Document Status
Single Document
Check the status of a single document:
document_status = cg.get_anonymized_document_status(document_id)
print(document_status)
Parameters
- document_id: The ID of the document.
Returns
- The status code (e.g., "success", "failed") if successful,
Noneif an error occurs.
Multiple Documents
Check the status of multiple documents at once:
document_ids = [document_id1, document_id2]
statuses = cg.get_anonymized_documents_status(document_ids)
print(statuses)
Parameters
- document_ids: A list of document IDs to check the status for.
Returns
- A list of dictionaries, each containing:
"document_id": Unique identifier for the document."status": Translation outcome, either"fail"or"success"."status_code": Numerical status code (e.g.,2for finished,0for queued)."status_name": Status description (e.g.,"finished","queued").
- In case of an error, the function will return
None.
Document Collections
Create a new document collection:
collection_id = cg.create_collection(name="My Collection", description="A collection of legal documents")
print(f"Collection created with ID: {collection_id}")
Upload a document to the collection:
upload_success = cg.upload_to_collection(collection_id, "/path/to/document.pdf")
print(f"Upload successful: {upload_success}")
Heartbeat
Fetch the health status of various system components:
heartbeat_status = cg.heartbeat()
print(heartbeat_status)
Parameters:
- None
Returns:
- Dictionary: A dictionary with the status of each component. Each key represents a system component (e.g.,
"SQL Server","Oracle DB") and contains the following fields:- status: The health status of the component, typically
"UP"or"DOWN". - message: A message providing more details about the component's health.
- sinceTimestamp: A timestamp indicating when the status was last updated.
- status: The health status of the component, typically
health_status = cg.healthcheck()
print(health_status)
Parameters:
- None
Returns:
- List of Tuples: The returned list contains tuples where each tuple represents a system resource and its health status. Each tuple contains:
- resource name: The name of the resource (e.g.,
"SQL Server","Oracle DB","Training GRPC","Triton Grpc Server"). - status: The health status of the resource, which can be either
"UP"(healthy) or"DOWN"(unhealthy).
- resource name: The name of the resource (e.g.,
Example Response Structure:
[
("SQL Server", "UP"),
("Oracle DB", "UP"),
("Training GRPC", "DOWN"),
("Triton Grpc Server", "UP")
]
Text Extraction
Extract paragraphs from raw text:
paragraphs = cg.text_extraction("This is a sample text that needs to be divided into paragraphs.")
print(paragraphs)
Extract text from a document file:
extracted_text = cg.text_extraction_from_document("/path/to/document.pdf")
print(extracted_text)
Text Embedding
Generate embeddings from raw text:
result = cg.get_text_embedding("This is a sample text.")
print(result)
Contributing
Contributions are welcome! Please fork the repository and submit a pull request.
License
This project is licensed under the MIT License.
Metadata
Release files for castleguard-sdk 0.31
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| castleguard_sdk-0.31.tar.gz | 29.5 kB | Without provenance | Details for castleguard_sdk-0.31.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| castleguard_sdk-0.31-py3-none-any.whl | Python 3 | none | any | Details for castleguard_sdk-0.31-py3-none-any.whl |
Total release size: 60.9 kB
Release files / castleguard_sdk-0.31.tar.gz
| Download URL | castleguard_sdk-0.31.tar.gz |
|---|---|
| Size | 29.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae7bb4252dc77e886927c12fee3ae6ed4b9b4b41c1949506152c5cebda581586
|
|
BLAKE2b-256 checksum How to use checksums |
cfd403a4a33c7139991ebad20b69d52acf99de777250d6e69e366c451bff4ce7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Release files / castleguard_sdk-0.31-py3-none-any.whl
| Download URL | castleguard_sdk-0.31-py3-none-any.whl |
|---|---|
| Size | 31.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1fb031116574081e0cc0370ae00ddc55c6c913b841280eca51aca3c80d81bbb0
|
|
BLAKE2b-256 checksum How to use checksums |
c70f689d480b35e16a82a65b7d158985ae555109b05258d9ce51190d674f7f8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.8
|
Security for castleguard-sdk 0.31
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.