def add_or_replace_documents(
        index_name: str,
        body_dict: dict,
        marqo_config: config.Config = Depends(get_config),
        device: str = Depends(api_validation.validate_device)):
    """
    Add an array of documents or replace them if they already exist.
    Please refer to [Add documents API](https://docs.marqo.ai/latest/reference/api/documents/add-or-replace-documents/)
    for details.
    """
    # TODO this a temporary fix due to the mixed use of pydantic v1 and v2.
    #  AddDocsBodyParams can be injected after migrated to v2
    body = parse_request_object(AddDocsBodyParams, body_dict)
    add_docs_params = api_utils.add_docs_params_orchestrator(index_name=index_name, body=body,
                                                             device=device)

    with RequestMetricsStore.for_request().time(f"POST /indexes/{index_name}/documents"):
        res = marqo_config.document.add_documents(add_docs_params=add_docs_params)
        return JSONResponse(content=res.dict(exclude_none=True, by_alias=True), headers=res.get_header_dict())

    Add an array of documents or replace them if they already exist.
    Please refer to [Add documents API](https://docs.marqo.ai/latest/reference/api/documents/add-or-replace-documents/)
    for details.
    
src/marqo/tensor_search/api.py